ULAM Bizarre #2 - The power of HP 42S STO/RCL arithmetic - Printable Version +- HP Forums (https://archived.hpcalc.org/museumforum) +-- Forum: HP Museum Forums (https://archived.hpcalc.org/museumforum/forum-1.html) +--- Forum: Old HP Forum Archives (https://archived.hpcalc.org/museumforum/forum-2.html) +--- Thread: ULAM Bizarre #2 - The power of HP 42S STO/RCL arithmetic (/thread-178428.html) |
ULAM Bizarre #2 - The power of HP 42S STO/RCL arithmetic - Allen - 02-05-2011 I was doodling yesterday in meeting, and found a closed-form solution to the 2008 Ulam Spiral mini-challenge that has been haunting me for three years from: this thread.
Usage: Enter a Number (N) and execute the program. It will return the non-trivial solutions for adjacent numbers in Ulam's spiral. 17 yields 36, 38
This program has a very concise check to see if Otherwise it returns and This closed form approach allows a 30% reduction in program space, AND an order of magnitude reduction in runtime. The new program is O(1) constant runtime, 37 bytes (28 steps max for all values with no looping)- The previous version from 2008 was O(N) and 54 bytes and 43 program lines with some considerable sized loops.
' 37-byte HP 42S ULAM spiral solution Allen T. 2/5/2011 Thanks to Thomas for his tutorial on using formulas. I did not follow the teacher exactly, but managed to get the formulas here anyway. * NOTE - I do not believe this program is compatible with the HP41C due to the specific use of the linear stat register and the STO/RCL arithmetic (line 15). Caveat Emptor.
Five Reasons I think this is a nice demonstration program for the 42S and RPN flexibility: 1) Lines 04 and 09 allow use of STACK(T) copies as the lower stack items are consumed in operations
Edited: 5 Feb 2011, 11:11 p.m.
Re: ULAM Bizarre #2 - The power of HP 42S STO/RCL arithmetic - Don Shepherd - 02-06-2011 Amazing work, Allen, congratulations. I was thinking about entering this code on my 32sii but I notice several references to things like STO * STO T and STO - STO Z. I assume these are 42s commands to put values directly on the stack, correct? And I don't believe these commands exist on the 32sii, is that correct? Many thanks for this great work in algorithm development.
Re: ULAM Bizarre #2 - The power of HP 42S STO/RCL arithmetic - Thomas Klemm - 02-06-2011 Hi Allen
You could reverse clearing the statistic registers and filling the stack thus saving one precious byte: 02 ENTER @ Fill the stack up with input Clearing the statistic registers enables the stack lift. Since 3 and 5 follow only one ENTER is needed. Furthermore you could replace the two last statements with SUM. This instruction uses two bytes as well, so no gain with that change, but this makes the program independent of the setting of \GSREG. However this instruction is missing in the HP-41C.
Quote: Did you just save the images or did you find another ingenious way? In the latter case I could add it to the article. Which reminds me to add Egan's method he once posted as well. It's a clever idea to use the statistic functions for some kind of parallel processing.
Kind regards
Edited: 6 Feb 2011, 5:13 a.m.
Re: ULAM Bizarre #2 - The power of HP 42S STO/RCL arithmetic - Dieter - 02-06-2011 Quote: The HP41-series offers direct access to the stack registers - they can be used in the same way as the usual numbered registers. So STO Z directly stores X in stack register Z, or ST+ T adds X to register T. This allows very effective programming and elegant solutions like ST+ X for doubling a number (even without affecting LastX). The 42s adds two more features: Since it can also use named storage registers a simple "STO T" might be confusing because it may refer to stack register T as well as a variable named T. That's why there's an additional "ST" for "stack" in commands referring to stack registers: STO T stores a value in variable T, while STO ST T stores it in the top stack tegister T. On the other hand, the 42s can also do all this with recall-arithmetics as well: RC+ ST Z adds the content of stack register Z to the value in X without affecting the stack otherwise. No, all this can't be done on the 32s and similar calculators.
Dieter
Re: ULAM Bizarre #2 - The power of HP 42S STO/RCL arithmetic - Allen - 02-06-2011 Thomas,
Don noted ULAM shows a correlation between meeting doodles and math.. perhaps I should go to more meetings!
Re: ULAM Bizarre #2 - The power of HP 42S STO/RCL arithmetic - Mark Storkamp - 02-06-2011 I made it work with the 41C by changing line 15 from
15 RCL+ ST T to
15 R^ Re: ULAM Bizarre #2 - down to 32 bytes - Allen - 02-07-2011 Mark, Thanks for your 41C Fix!! I made the output from FREE42 and used HP41UC to create a 41C barcode, then scanned it in using my wand- sure it may have been faster just to type the thing in, but I'm not sure how to type R^ in the 41c. Thomas, I incorporated your Stack lift saving 1 byte and SUM features, I also found a way to save 4 more bytes by getting rid of some of the 2-Byte STO+ instructions from above. Now it's down to 32 Bytes- Not sure I can do much better than that.
Re: ULAM Bizarre #2 - down to 32 bytes - Thomas Klemm - 02-07-2011 Quote: Special characters are listed on the back of the calculator:
So the keystrokes would be:
HTH |