RPL MicroChallenge: Christmas in July

07172017, 02:26 PM
Post: #1




RPL MicroChallenge: Christmas in July
Everybody knows the "12 Days of Christmas" Song (Partridge in a Pear Tree etc). How many gifts has my true love given to me altogether by the end of Day 3? Well, the sum of today's 6 gifts (3+2+1) plus yesterday's 3 gifts (2+1) plus the first day's 1 gift comes to 10 gifts in all. So the Total Gifts through Day 3 = TG(3) = 10.
What is the smallest possible (bytecount) User RPL program that calculates TG(x)? To check your program, here are a few sample inputs and outputs: TG(3)=10 TG(5)=35 TG(12)=364 TG(365)=8171255 N.B. This is called a microchallenge instead of a minichallenge because a REALLY SMALL solution exists. If it's obvious to you, please don't post it right away; let others try to find it first. "We only appreciate the answer if we struggle with the question."  Ignatius O'Brien, O.P. <0ɸ0> Joe 

07172017, 02:53 PM
(This post was last modified: 07172017 05:00 PM by Werner.)
Post: #2




RE: RPL MicroChallenge: Christmas in July
I still know the formula by heart, and its shorter form.
4 commands, 2 of which are single digit numbers ;) That would then be 20 bytes including the Rpl markers << >> Cheers, Werner 

07172017, 03:45 PM
Post: #3




RE: RPL MicroChallenge: Christmas in July  
07172017, 04:09 PM
Post: #4




RE: RPL MicroChallenge: Christmas in July
32.5 bytes. Only first attempt, though.


07172017, 04:32 PM
(This post was last modified: 07172017 04:57 PM by Gerson W. Barbosa.)
Post: #5




RE: RPL MicroChallenge: Christmas in July
More like it: 27.5 bytes.
PS: Even more like it: 20 bytes. 

07172017, 04:54 PM
Post: #6




RE: RPL MicroChallenge: Christmas in July
Great question, Joe! Finite differences quickly gave me the formula; factoring it gave a simplified form (also 4 operations and two single digit constants). Several attempts keep giving me 9 step solutions. Do you have a shorter one? (RPN form used; LBL and RTN not counted)


07172017, 05:58 PM
Post: #7




RE: RPL MicroChallenge: Christmas in July  
07172017, 06:52 PM
Post: #8




RE: RPL MicroChallenge: Christmas in July
32.5 & stuck.


07172017, 08:17 PM
Post: #9




RE: RPL MicroChallenge: Christmas in July  
07172017, 08:31 PM
(This post was last modified: 07172017 08:55 PM by Gilles59.)
Post: #10




RE: RPL MicroChallenge: Christmas in July
(07172017 08:17 PM)Gerson W. Barbosa Wrote:(07172017 06:52 PM)Gerald H Wrote: 32.5 & stuck. Yes but I cannot imagine how you arrive at 20 bytes ... Did you have a division in your expression ? [EDIT] Eureka! I get it! 

07172017, 09:33 PM
Post: #11




RE: RPL MicroChallenge: Christmas in July  
07182017, 12:04 AM
(This post was last modified: 07182017 12:24 AM by Joe Horn.)
Post: #12




RE: RPL MicroChallenge: Christmas in July
(07172017 04:54 PM)Jim Horn Wrote: Great question, Joe! Finite differences quickly gave me the formula; factoring it gave a simplified form (also 4 operations and two single digit constants). Several attempts keep giving me 9 step solutions. Do you have a shorter one? (RPN form used; LBL and RTN not counted) This is an RPL challenge, but please feel free to implement it for the RPN machine of your choice. I wonder whether the polynomial form or the shorter form would be faster on a machine that doesn't have the secret command that's needed (such as the barebones HP41). (07172017 09:33 PM)Gerson W. Barbosa Wrote: Apparently the 20byte record cannot be broken. Yes, I'm sure that we're all arriving at the same 20byte solution. Final hint to those still hunting: the formula can be expressed this way, which should remind you of a shorter way of expressing it: \(\frac { x(x+1)(x+2) }{ 1\cdot 2\cdot 3 } \) <0ɸ0> Joe 

07182017, 02:17 AM
Post: #13




RE: RPL MicroChallenge: Christmas in July
7 steps (not including LBL and RTN) is the shortest I can get it so far on the 20S.


07182017, 04:24 AM
Post: #14




RE: RPL MicroChallenge: Christmas in July
Four steps on the 34S.
Pauli 

07182017, 06:31 PM
Post: #15




RE: RPL MicroChallenge: Christmas in July
Ausgezeichnet! I had "6" in the denominator so didn't recognize the nowobvious simplification as per Paul's note. Many thanks, Joe  a clever insight on your part!


07182017, 09:07 PM
(This post was last modified: 07182017 09:33 PM by Gerson W. Barbosa.)
Post: #16




RE: RPL MicroChallenge: Christmas in July
(07172017 05:58 PM)Gilles59 Wrote:(07172017 04:32 PM)Gerson W. Barbosa Wrote: More like it: 27.5 bytes. Now that you've found it I should tell you my next program yesterday was 38.5 bytes long. An exotic memoryconsuming solution: « x x x IDN →DIAG x x x x » I know there's a better equivalent of the two middle commands, but I don't remember which. Don't anyone be misled by this one. Joe's optimum solution is simply « y y y y » :) 

07192017, 09:41 AM
Post: #17




RE: RPL MicroChallenge: Christmas in July
All well & good saying you have a solution with so many bytes but eventually you lay your cards on the table.
Below a 32.5 Byte solution to the problem: Code:


07192017, 11:50 AM
Post: #18




RE: RPL MicroChallenge: Christmas in July
A 38 bytes solution
Code: << [ 1 3 2 0 ] SWAP PEVAL 6 / >> 

07192017, 12:56 PM
(This post was last modified: 07192017 01:47 PM by Joe Horn.)
Post: #19




RE: RPL MicroChallenge: Christmas in July
(07192017 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 20byte 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(x1)(x2) }{ 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. <0ɸ0> Joe 

07192017, 01:42 PM
Post: #20




RE: RPL MicroChallenge: Christmas in July
Oh, duh, I was doing COMB(x+2, x1), completely forgetting about the symmetric nature of combinations.


« Next Oldest  Next Newest »

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