The Museum of HP Calculators

HP Forum Archive 21

 Re: RPN Programming exercise (HP-42S)Message #1 Posted by Gerson W. Barbosa on 27 Feb 2012, 1:55 p.m. Thanks all of you for your participation and interesting solutions. Most of them supersede mine, as they are shorter, faster and accept a wider range of operands, exactly what I expected them to be. Here are my solutions: ```00 { 77-Byte Prgm } 01>LBL "MLTA" 02 0 03 0.099 04 Rv 05>LBL 00 06 STO IND ST T 07 RCL+ ST Y 08 ISG ST T 09 GTO 00 10 RCL ST Z 11 CLA 12 AIP 13 XEQ 02 14 XEQ 02 15 RCL IND ST Y 16 RCL IND ST Y 17 1.1 18 Rv 19>LBL 01 20 RCL+ ST Y 21 ISG ST T 22 GTO 01 23 RTN 24>LBL 02 25 ATOX 26 RCL+ ST X 27 LASTX 28 -3 29 ROTXY 30 + 31 ATOX 32 + 33 528 34 - 35 END ``` This will accept two numbers in the range [1000..9999] and give their product. Examples: ```1234 ENTER 5678 XEQ MLTA --> 7,006,652 9999 ENTER XEQ MLTA --> 99,980,001 ``` Here is an equivalent QBASIC program: ``` CLS DEFINT A-D, I DEFDBL S DIM M(100) INPUT A, B C = VAL(MID\$(STR\$(B), 2, 2)): REM C = B \ 100 D = VAL(RIGHT\$(STR\$(B), 2)): REM D = B - 100 * C S = 0 FOR I = 0 TO 99 M(I) = S S = S + A NEXT I S = M(D) FOR I = 1 TO 100 S = S + M(C) NEXT I PRINT S END ? 1234,5678 7006652 ``` The next program is an implementation of the Russian Peasant Multiplication Method: ```00 { 52-Byte Prgm } 01>LBL "MLTB" 02 0 03 STO 00 04 Rv 05>LBL 00 06 0 07 BIT? 08 XEQ 01 09 BASE+ 10 X=0? 11 GTO 02 12 1 13 ROTXY 14 X<>Y 15 -1 16 ROTXY 17 X<>Y 18 GTO 00 19>LBL 01 20 NOT 21 BASE+ 22 X<>Y 23 STO+ 00 24 X<>Y 25 0 26 RTN 27>LBL 02 28 RCL 00 29 .END. ``` This will give the product of any two positive integer numbers, as long as the result doesn't exceed 236-1. Examples: ```12 ENTER 3456789 XEQ MLTB --> 41,481,468 1234 ENTER 5678 XEQ MLTB --> 7,006,652 262143 ENTER XEQ MLTB --> 68,718,952,449 ``` Here is a variation of the first program, in case one of the operands is allowed to be entered as two separate two-digit numbers, as proposed somewhere else in this thread: ```00 { 47-Byte Prgm } 01>LBL "MLTC" 02 STO 01 03 0 04 STO 00 05 CLX 06 2.099 07 Rv 08>LBL 00 09 RCL+ 01 10 STO IND ST T 11 ISG ST T 12 GTO 00 13 RCL IND ST Z 14 RCL IND ST Z 15 1.1 16 Rv 17>LBL 01 18 RCL+ ST Y 19 ISG ST T 20 GTO 01 21 END ``` Examples: ```12 ENTER 34 ENTER 5678 XEQ MLTC --> 7,006,652 99 ENTER ENTER 9999 XEQ MLTC --> 99,980,001 ``` Gerson. P.S.: Programs #1 and #3 require the allocation of 100 numbered registers: `Shift MODES \/ SIZE 0100 ` Edited: 27 Feb 2012, 5:39 p.m.

 Re: RPN Programming exercise (HP-42S)Message #2 Posted by Marcus von Cube, Germany on 27 Feb 2012, 5:51 p.m.,in response to message #1 by Gerson W. Barbosa I was missing AIP. Used instead of ARCL it would have saved the problems with the 1000s separator or the decimal point. Otherwise, my program wouldn't need big changes.

Go back to the main exhibit hall