Post Reply 
RPL Micro-Challenge: Christmas in July
07-19-2017, 12:56 PM (This post was last modified: 07-19-2017 01:47 PM by Joe Horn.)
Post: #19
RE: RPL Micro-Challenge: Christmas in July
(07-19-2017 09:41 AM)Gerald H Wrote:  All well & good saying you have a solution with so many bytes but eventually you lay your cards on the table.

Ok, we are far enough down the page to reveal the 20-byte solution:

« 2 + 3 COMB »

Why it works: The formula for TG(x) is \(\frac { x(x+1)(x+2) }{ 1\cdot 2\cdot 3 }\), but that looks very similar to the formula for COMB(x,3) which is \(\frac { x(x-1)(x-2) }{ 1\cdot 2\cdot 3 }\). All you have to do to turn the latter into the former is add 2 to x, and voila, TG(x) = COMB(x+2,3).

This is an example of program optimization being obtained not by code optimization but by math optimization.

EDIT: I just noticed something cool. If you run the program above on an input of 'X' (undefined), and then EVAL the resulting mess, you get this:

\[\frac { { X }^{ 3 }+3{ X }^{ 2 }+2X }{ 6 }\]
Now press FACTOR (or COLLECT) and see this:
\[\frac { X\cdot (X+1)\cdot (X+2) }{ 3\cdot 2 }\]
It almost does all the thinking for you. Big Grin

<0|ɸ|0>
-Joe-
Visit this user's website Find all posts by this user
Quote this message in a reply
Post Reply 


Messages In This Thread
RE: RPL Micro-Challenge: Christmas in July - Joe Horn - 07-19-2017 12:56 PM



User(s) browsing this thread: 2 Guest(s)