S&SMC#9: Optimized 42S (and 41C) RPN solution



#2

Hi,

The RPN solution I gave for the 42S yesterday was an
unoptimized, direct translation of my original solution for the 71B, and would run only on a 42S because it used more subroutine levels than the 41C would allow.

On the other and, this optimized translation, despite still using the exact same logic as the 71B version, does include the following improvements over the first one:

  • Appreciably shorter (80 steps, 149 bytes vs. 92 steps, 167 bytes) and faster.

  • Doesn't use subroutine levels in a recursive manner, so its maximum depth is 1, and thus will run on the HP-41C essentially unchanged.
The only modification necessary to make it run in the HP-41C is to expand the RCL arithmetic at steps 44 (RCL+ IND 01) and 53 (RCLx IND 03) into two separate steps each (RCL, + and RCL, x, respectively), thus the 41C version will be 82 steps long.

01*LBL "SR" 21 STO IND 02 41 FS? IND ST X 61 3
02 0.009 22 CF 15 42 GTO 03 62 XEQ 06
03 STO 04 23 X=Y? 43 INT 63 RUP
04*LBL 05 24 SF 15 44 RCL+ IND 01 64 GTO 00
05 CF IND ST X 25 3 45 STO IND 03 65*LBL 04
06 ISG ST X 26 FC? 15 46 RCL 00 66 RCL IND 02
07 GT0 05 27 STO- IND 02 47 MOD 67 CF IND ST X
08 1 28 1E5 48 X#0? 68*LBL 03
09 STO 00 29 STO/ IND 02 49 GTO 03 69 ISG IND 02
10 5 30 RCL 04 50 RCL IND 02 70 GTO 01
11 STO 01 31 STO+ IND 02 51 SF IND ST X 71 -3
12 6 32 4 52 10 72 XEQ 06
13 STO 02 33 FS? 15 53 RCLx IND 03 73 GTO 04
14 7 34 STO+ IND 02 54 9 74*LBL 06
15 STO 03 35 RCL 00 55 RCL 00 75 STO+ 01
16 0 36 2 56 X<Y? 76 STO+ 02
17*LBL 00 37 MOD 57 GTO 02 77 STO+ 03
18 STO IND 01 38 STO+ IND 02 58 VIEW ST Z 78 SIGN
19 RCL 00 39*LBL 01 59 GTO 04 79 STO+ 00
20 5 40 RCL IND 02 60*LBL 02 80 END

To run it, simply:
    SIZE 32
SF 21
XEQ "SR" -> ST Z=3,816,547,290 (in the HP42S)
-> 3,816,547,290 (in the HP-41C)
after a few seconds in the 42S or a few minutes in the 41C. No other solutions do exist, but you can make certain by
continuing the search by pressing [R/S]. After a while it will signal that further solutions are nonexistent by displaying "Nonexistent" (42S) or "NONEXISTENT" (41C),
which is achieved by forcing that error message when the search is over, thus saving the steps required to display an alpha prompt.

This same program would also run in an HP-15C (despite the effects of using system flags 8 and 9) with a small change to cater for the fact that you can't use the X-register for indirect addressing (simply X<>I, indirect operation using I, X<>I) and also bringing the solution to the X-register before pausing/stopping for the user to see it (the 42S/41C version above displays the solution using a VIEW Z, because it is in the Z-register at that time).

Best regards from V.


Possibly Related Threads…
Thread Author Replies Views Last Post
  [HP-PIRME] BUG Pretty Print, Solution: abs => |...|, ABS => ||...|| CompSystems 2 2,157 12-13-2013, 09:36 AM
Last Post: CompSystems
  Converting Great Circle Navigation from 41C to 42S Bill Triplett 11 3,461 11-13-2013, 07:24 AM
Last Post: Kimberly Thompson
  [PRIME] RPN: another attempt at returning more than one value to the RPN stack Marcus von Cube, Germany 5 2,465 11-05-2013, 02:44 AM
Last Post: Marcus von Cube, Germany
  [HP-Prime xCAS] Arrays: Programming Commands (solution to the ambiguity) CompSystems 6 2,622 08-30-2013, 06:32 PM
Last Post: CompSystems
  HP-Prime: Solution to ambiguity with [ ... ] & another BUGs CompSystems 12 3,690 08-28-2013, 08:48 AM
Last Post: CompSystems
  Advanced User's Manual and solution to the ambiguity of data types [HP-Prime xCAS] CompSystems 15 5,539 08-20-2013, 03:37 PM
Last Post: Thomas Klemm
  HHC 2012 RPN Programming Contest - 10-Step Way-After-Nashville Solution Jeff O. 32 6,879 10-12-2012, 01:41 AM
Last Post: Paul Dale
  41CL: Linux/Mac connection solution (soon) Geir Isene 28 6,502 06-10-2012, 01:21 PM
Last Post: Ángel Martin
  Re: RPN Programming exercise (HP-42S) Gerson W. Barbosa 1 1,036 02-27-2012, 05:51 PM
Last Post: Marcus von Cube, Germany
  RPN Programming exercise (HP-42S) Gerson W. Barbosa 71 13,633 02-26-2012, 11:48 AM
Last Post: Valentin Albillo

Forum Jump: