Re: RPN Programming exercise (HP-42S)



#3

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.


#4

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.


Possibly Related Threads...
Thread Author Replies Views Last Post
  [PRIME] RPN: another attempt at returning more than one value to the RPN stack Marcus von Cube, Germany 5 845 11-05-2013, 02:44 AM
Last Post: Marcus von Cube, Germany
  HHC 2013 RPN Programming Challenge - Final Thought Jeff O. 7 861 10-17-2013, 11:02 AM
Last Post: Jeff O.
  HHC / HP Museum Programming Contest for RPN and RPL machines Gene Wright 18 1,658 09-22-2013, 09:39 AM
Last Post: Miguel Toro
  HP 42s programming help for novice Carl D (new) 4 493 02-02-2013, 11:39 PM
Last Post: Mike Morrow
  HHC 2012 RPN Programming Contest - 10-Step Way-After-Nashville Solution Jeff O. 32 2,624 10-12-2012, 01:41 AM
Last Post: Paul Dale
  HHC 2012 RPN Programming Challenge Conundrum Jeff O. 15 1,258 10-08-2012, 03:34 PM
Last Post: Gerson W. Barbosa
  HHC 2012 RPN Programming Contest Gene Wright 73 3,721 09-28-2012, 12:43 PM
Last Post: x34
  HHC 2012 programming contests coming soon (RPN and RPL) Gene Wright 9 949 09-21-2012, 03:38 PM
Last Post: Paul Dale
  Programming exercise Gerson W. Barbosa 32 2,132 08-23-2012, 07:42 PM
Last Post: Gerson W. Barbosa
  Help with RPN programming hpnut 36 2,526 03-03-2012, 09:31 AM
Last Post: Bill (Smithville, NJ)

Forum Jump: