A “built-in” primality test function and composite numbers factorisation

Contributions to this software library are always welcome. Please ensure that you post program listings rather than .raw files. They give a reasonable idea of what your program does without having to load them into a DM42 and you can also include comments in your code. Check out the following link for a decoder/encoder: https://technical.swissmicros.com/decoders/dm42/

You can then copy/paste the listing and post it in "code" tags.
Post Reply
Hawk45
Posts: 7
Joined: Fri Feb 17, 2023 2:45 pm
Location: Stockholm, Sweden

A “built-in” primality test function and composite numbers factorisation

Post by Hawk45 »

I bought my HP42S in the summer 1994 and it is still in excellent condition and works fine. Despite this, I recently bo
ught a DM42 ;)

Using Boubker’s primality test program (viewtopic.php?t=3073) as a base to explore the possibilities with the Free42 Extensions, I ended up with this program.
By the way, the thread briefly discusses a bug where the program, for instance, reports N=5 as NOT PRIME. This bug appears because this deterministic Milller-Rabin test does not work when a MR-base mod N = 0. The referenced Wikipedia article with the pseudo code fails to mention this.

My program works like a built-in conditional function; used in a program the next program line is skipped if test negative; executed from the keyboard it reports the test result with Yes/No. It alters no user flags or registers including the Alpha and Stack registers.

The test range is up to 1E17 to enjoy fast execution time (a couple of seconds on battery power regardless of number size in the range) and no need for code to handle overflow conditions. The biggest prime in the range is 99,999,999,999,999,997.

The program does not accept negative numbers and views the number 1 as neither prime nor composite in line with the common view among today’s mathematicians.

The two subroutines LBL20 (performs Miller-Rabin test for a MR-base) and LBL30 (calculates MR-base**D mod N with the fast Right-to-left binary method) might be somewhat hard to understand as they keep some variables in the stack instead of STO/RCLs. The rest of the program should be easy to understand.
The variables in stack are Result and D for LBL20 and Result and MR-base for LBL30.

The program uses the 4-level stack, but switches to the big stack to create a List of MR-bases, so “Dynamic Stack Extension” must be enabled in SETUP.

Code: Select all

00 { 192-Byte Prgm }
01▸LBL "PRM?"
02 FUNC 00
03 LSTO "N"
04 REAL?
05 SKIP
06 RTNERR 4
07 ENTER
08 IP
09 X≠Y?
10 RTNERR 4
11 1ᴇ17
12 X<Y?
13 RTNERR 2
14 R↓
15 NSTK
16 2
17 3
18 5
19 7
20 11
21 13
22 17
23 19
24 23
25 9
26 →LIST
27 4STK
28 LSTO "W"
29 X<>Y
30 POS
31 X≥0?
32 RTNYES
33 +
34 X≤0?
35 RTNERR 5
36▸LBL 08
37 2
38 ÷
39 IP
40 LASTX
41 X=Y?
42 GTO 08
43 STO+ ST X
44 LSTO "D"
45▸LBL 09
46 HEAD "W"
47 SKIP
48 RTNYES
49 XEQ 30
50 SIGN
51 RCL "D"
52 R↓
53 X≠Y?
54 XEQ 20
55 GTO 09
56 RTNNO
57▸LBL 20
58 +/-
59 RCL+ "N"
60 X=Y?
61 RTN
62 R↑
63 X=Y?
64 RTNNO
65 STO+ ST X
66 X<> ST Z
67 X↑2
68 RCL "N"
69 MOD
70 1
71 X=Y?
72 RTNNO
73 GTO 20
74▸LBL 30
75 RCL "D"
76 LSTO "D"
77 SIGN
78▸LBL 10
79 RCL "D"
80 IP
81 X=0?
82 RTN
83 2
84 STO÷ "D"
85 MOD
86 R↓
87 GTO IND ST T
88▸LBL 01
89 ×
90 RCL "N"
91 MOD
92▸LBL 00
93 X<>Y
94 X↑2
95 RCL "N"
96 MOD
97 X<>Y
98 GTO 10
99 END
PRM-.raw
(195 Bytes) Downloaded 186 times
Last edited by Hawk45 on Fri Feb 24, 2023 7:24 pm, edited 1 time in total.
/Hakan

HP21, HP32SII,HP42S, HP48 SX,G,GX, DM42
Boub65
Posts: 231
Joined: Tue Sep 12, 2017 4:34 pm
Location: Rabat, Morocco

Re: A “built-in” primality test function

Post by Boub65 »

Nice work 👏
Sincèrement, Sincerely, 73,
Boubker

DM15L, DM41L, DM42 #00855 (domes upgraded), DM41X #00707
HP48SX (with dark screen), HP42s, HP32SII (1990 with fraction bug), HP41C/CV
TI-89 titanium, CASIO fx-cg50 and Numworks (to play with micropython)
User avatar
rudi
Posts: 415
Joined: Wed Nov 03, 2021 9:03 am
Location: Denmark
Contact:

Re: A “built-in” primality test function

Post by rudi »

Yes - nice work indeed 👍

But remember that the Miller–Rabin primality test or Rabin–Miller primality test is a probabilistic primality test: an algorithm which determines whether a given number is likely to be prime.
In other words, you do not get a deterministic answer, prime or not prime, but rather likely prime or not likely prime.

A Miller–Rabin primality test is usefull to quickly determine, if it is worth examening a large number with a deterministic and much slower algorithm.
/Rudi

DM-42 (s/n 06999), HP-42S, HP-35s, HP-11c, HP-32SII (ex HP-41CV, ex HP-75C, ex HP-48G + a lot, really lot of a accessories)
Denmark
Hawk45
Posts: 7
Joined: Fri Feb 17, 2023 2:45 pm
Location: Stockholm, Sweden

Re: A “built-in” primality test function

Post by Hawk45 »

Yes, the Miller-Rabin test is probablistic with the accuracy dependent on number of bases tested, but there are deterministic variants where choosing certain bases makes the test deterministic up to certain limits; See https://en.wikipedia.org/wiki/Miller–R ... ality_test.

The PRM? function uses 9 bases: 2,3,5,7,11,13,17,19,23 with a test range up to 1E17. With these bases, the test is deterministic for all N < 3,825,123,056,546,413,051, approx. = 3E18. So PRM? test results are deterministic, for sure ;)
/Hakan

HP21, HP32SII,HP42S, HP48 SX,G,GX, DM42
Hawk45
Posts: 7
Joined: Fri Feb 17, 2023 2:45 pm
Location: Stockholm, Sweden

Re: A “built-in” primality test function

Post by Hawk45 »

A useful companion to the PRM? function is Erik Ehrling’s HP42S factorisation program FCTR. See write-up: https://archived.hpcalc.org/hp42s/fctr.html

The program should run as-is on DM42, but with a few simple updates (see below) it can behave as follows when executed with a number in the X register:
  • If the number is a prime, it displays “Prime” in a few seconds.
  • If the number is composite, it displays all prime factors for the number. See Erik’s write-up for two examples.
In summary, I mainly made the following changes to FCTR:
  • Added a call to PRM? so the program quickly detects a prime. This also takes care of most of the initial checking that the number is valid.
  • Eliminated the use of register 00, so now all calculations are done in the stack. This also removes the need to save/recall the registers.
  • Extended the range from 1E8 to 1E12. The range can probably be further extended, but you might then hit some long execution times (See below).
The changes reduces the program footprint from 463 byes to 269 bytes.

The execution times on battery power for composite numbers are a few seconds as long as the number does not have very big prime factors.
An example: 999,999,999,999 executes in a few seconds while the smaller number 567,823,657,941 (=3^2*63,091,517,549) needs 40 seconds.
A possible improvement to get rid of long execution times for composite numbers might to to further use PRM? to detect useless looping when the program tries to find non-existent factors of a big prime number.

I attach the updated FCTR program.
/Hakan

HP21, HP32SII,HP42S, HP48 SX,G,GX, DM42
Hawk45
Posts: 7
Joined: Fri Feb 17, 2023 2:45 pm
Location: Stockholm, Sweden

Re: A “built-in” primality test function and composite numbers factorisation

Post by Hawk45 »

I now have an updated, faster version of the FCTR program.
An example: with worst case (see below) for the range up to 1E12, the execution time is reduced from 2min 42sec to 2min 30sec. This is accomplished by replacing the LASTX, + instructions in the factorisation wheel (the LBL 03 loop) with RCL+ ST L, thereby saving 8 stack-lifts, stack-drops for each turn of the wheel while traveling along the number line.
The RCL+ ST L is a 3-byte instruction, so the wheel footprint is increased by 8 bytes. This is offset by eliminating the need for Flag 00, so the resulting footprint of this version is slightly less then the old one.

The range limit in the program is set to 1E12, but you can decrease/increase it to your liking. The execution time on battery power is normally a few seconds, but there is a worst case when the tested number has 2 equally sized, big prime factors that fit in the range as follows:
  • 1E9: 31,607x31,607 5sec
  • 1E10: 99,901x99,901 16sec
  • 1E11: 316,223x316,223 48sec
  • 1E12: 999,983x999,983 2min 30sec

Code: Select all

00 { 262-Byte Prgm }
01▸LBL "FCTR"
02 FUNC 00
03 REAL?
04 SKIP
05 RTNERR 4
06 ENTER
07 ABS
08 "Prime"
09 XEQ "PRM?"
10 GTO 04
11 CLA
12 X≠Y?
13 "-"
14 1ᴇ12
15 X<Y?
16 RTNERR 2
17 GTO 00
18▸LBL 20
19 X<>Y
20 ENTER
21 ISG ST Z
22 NOP
23 ENTER
24 ANUM
25 X≠Y?
26 ├"*"
27 R↓
28 LASTX
29 AIP
30 ÷
31 STO ST Y
32 LASTX
33 MOD
34 X≠0?
35 RTN
36 ├"↑"
37▸LBL 02
38 X<>Y
39 STO ST Y
40 LASTX
41 ÷
42 ISG ST Z
43 NOP
44 STO ST Y
45 LASTX
46 MOD
47 X=0?
48 GTO 02
49 X<>Y
50 R↑
51 AIP
52 RTN
53▸LBL 00
54 R↓
55 STO ST Y
56 2
57 MOD
58 X=0?
59 XEQ 20
60 X<>Y
61 STO ST Y
62 3
63 MOD
64 X=0?
65 XEQ 20
66 X<>Y
67 STO ST Y
68 5
69 MOD
70 X=0?
71 XEQ 20
72 X<>Y
73 STO ST Y
74 7
75 MOD
76 X=0?
77 XEQ 20
78▸LBL 03
79 X<>Y
80 STO ST Y
81 4
82 RCL+ ST L
83 MOD
84 X=0?
85 XEQ 20
86 X<>Y
87 STO ST Y
88 2
89 RCL+ ST L
90 MOD
91 X=0?
92 XEQ 20
93 X<>Y
94 STO ST Y
95 4
96 RCL+ ST L
97 MOD
98 X=0?
99 XEQ 20
100 X<>Y
101 STO ST Y
102 2
103 RCL+ ST L
104 MOD
105 X=0?
106 XEQ 20
107 X<>Y
108 STO ST Y
109 4
110 RCL+ ST L
111 MOD
112 X=0?
113 XEQ 20
114 X<>Y
115 STO ST Y
116 6
117 RCL+ ST L
118 MOD
119 X=0?
120 XEQ 20
121 X<>Y
122 STO ST Y
123 2
124 RCL+ ST L
125 MOD
126 X=0?
127 XEQ 20
128 X<>Y
129 STO ST Y
130 6
131 RCL+ ST L
132 MOD
133 X=0?
134 XEQ 20
135 X<>Y
136 LASTX
137 X↑2
138 X≤Y?
139 GTO 03
140 SIGN
141 X<>Y
142 X=Y?
143 GTO 04
144 ├"*"
145 AIP
146▸LBL 04
147 AVIEW
148 END
FCTR.raw
(265 Bytes) Downloaded 168 times
/Hakan

HP21, HP32SII,HP42S, HP48 SX,G,GX, DM42
Hawk45
Posts: 7
Joined: Fri Feb 17, 2023 2:45 pm
Location: Stockholm, Sweden

Re: A “built-in” primality test function and composite numbers factorisation

Post by Hawk45 »

I have updated the FCTR factorisation program with Pollard’s Rho algorithm for big numbers. This makes a big difference with much faster execution times; for instance: the example in my previous post that executed in 2 min 30 sec now takes 9 sec. As the new program is so fast, I have removed the 1E12 limitation on the test range; you can now test numbers up to 1E17.
To distinguish it from the old program FCTR I have renamed it to PFCT (short for Prime Factorisation).

Code: Select all

00 { 64-Byte Prgm }
01▸LBL "Prho"
02 FUNC 01
03 LSTO "N"
04 2
05 ENTER
06 GTO 00
07▸LBL 10
08 X↑2
09 1
10 +
11 RCL "N"
12 MOD
13 RTN
14▸LBL 02
15 R↓
16 R↓
17 X<>Y
18▸LBL 00
19 XEQ 10
20 X<>Y
21 XEQ 10
22 XEQ 10
23 DUPN 2
24 -
25 ABS
26 RCL "N"
27▸LBL 01
28 MOD
29 LASTX
30 X<>Y
31 X≠0?
32 GTO 01
33 SIGN
34 X=Y?
35 GTO 02
36 X<>Y
37 END
Pollard’s algorithm is implemented in the small program “Prho”. It can be used on its own by entering a number N an XEQ the program. It will return N in the y-reg and a found prime factor in the x-reg. You can then press divide and XEQ again to get the next factor and so on. But there is a caveat: the algorithm can in rare cases fail and return a composite factor instead of a prime factor.
But the main purpose of Prho is the integration with PFCT. This integration will also detect and handle cases when Pollard Rho fails, so the output will always be correct (see example below).

Now to PFCT factorisation:
Usage
Enter a number <= 1E17 and XEQ PFCT. Example (executes in 6 sec):
Number 99,999,999,999,999,945 has these prime factors:
PFCT-exmp.bmp
PFCT-exmp.bmp (12.31 KiB) Viewed 2647 times
A “constructed” example where Pollard’s algorithm partly fails (executes in 1 sec): Number 50,428,999,949,571 gives this result:
PFCT_rhofail.bmp
PFCT_rhofail.bmp (12.31 KiB) Viewed 2647 times
Note that the factor 50429 has a question mark. This means that the factor is composite, so you need to separately XEQ PFCT with this number to get the remaining prime factors 211 and 239.

Technical notes
  • Execution times (on battery power) varies a lot. The max number 1E17 executes in a second, while the number 1E17 - 45 needs 4 min 18 sec. The execution time mainly depends on the size of the second largest prime factor in the number. If this factor has 6-digits (corresponding to a N up to 1E12) execution time will be less than 10 sec. With 7-9 digits the time will normally lie in the range 30 sec - 2 min, but occasionally higher as in the example above. This is currently the highest time I have found; if you can find higher times please post your findings.
  • The math behind PFCT is:
    • A Miller-Rabin primality test, deterministic in supported range.
    • Wheel factorisation with bases 2, 3, 5.
    • Pollard Rho/Floyd’s cycle-finding algorithm with f(x)=x^2 + c, x0=y0=2 and c=1.
  • Note that PFCT requires the PRM? and Prho programs.

Code: Select all

00 { 312-Byte Prgm }
01▸LBL "PFCT"
02 FUNC 00
03 REAL?
04 SKIP
05 RTNERR 4
06 ENTER
07 ABS
08 "Prime"
09 XEQ "PRM?"
10 GTO 04
11 CLA
12 X≠Y?
13 "-"
14 GTO 00
15▸LBL 20
16 X<>Y
17 ENTER
18 ISG ST Z
19 NOP
20 ENTER
21 ANUM
22 X≠Y?
23 ├"*"
24 R↓
25 LASTX
26 AIP
27 ÷
28 STO ST Y
29 LASTX
30 MOD
31 X≠0?
32 RTN
33 ├"↑"
34▸LBL 02
35 X<>Y
36 STO ST Y
37 LASTX
38 ÷
39 ISG ST Z
40 NOP
41 STO ST Y
42 LASTX
43 MOD
44 X=0?
45 GTO 02
46 X<>Y
47 R↑
48 AIP
49 RTN
50▸LBL 00
51 STO ST Y
52 2
53 MOD
54 X=0?
55 XEQ 20
56 X<>Y
57 STO ST Y
58 3
59 MOD
60 X=0?
61 XEQ 20
62 X<>Y
63 STO ST Y
64 5
65 MOD
66 X=0?
67 XEQ 20
68 X<>Y
69 STO ST Y
70 7
71 MOD
72 X=0?
73 XEQ 20
74▸LBL 03
75 X<>Y
76 STO ST Y
77 4
78 RCL+ ST L
79 MOD
80 X=0?
81 XEQ 20
82 X<>Y
83 STO ST Y
84 2
85 RCL+ ST L
86 MOD
87 X=0?
88 XEQ 20
89 X<>Y
90 STO ST Y
91 4
92 RCL+ ST L
93 MOD
94 X=0?
95 XEQ 20
96 X<>Y
97 STO ST Y
98 2
99 RCL+ ST L
100 MOD
101 X=0?
102 XEQ 20
103 X<>Y
104 STO ST Y
105 4
106 RCL+ ST L
107 MOD
108 X=0?
109 XEQ 20
110 X<>Y
111 STO ST Y
112 6
113 RCL+ ST L
114 MOD
115 X=0?
116 XEQ 20
117 X<>Y
118 STO ST Y
119 2
120 RCL+ ST L
121 MOD
122 X=0?
123 XEQ 20
124 X<>Y
125 STO ST Y
126 6
127 RCL+ ST L
128 MOD
129 X=0?
130 XEQ 20
131 X<>Y
132 LASTX
133 X↑2
134 X>Y?
135 GTO 05
136 R↓
137 4ᴇ9
138 X>Y?
139 GTO 03
140 R↓
141 107
142 X>? ST L
143 GTO 03
144▸LBL 06
145 X<>Y
146 XEQ "PRM?"
147 GTO 07
148 XEQ "Prho"
149 STO ST L
150 CLX
151 XEQ 20
152 LASTX
153 XEQ "PRM?"
154 SKIP
155 ├"?"
156 R↓
157 SIGN
158 X≠Y?
159 GTO 06
160▸LBL 05
161 SIGN
162 X<>Y
163 X=Y?
164 GTO 04
165▸LBL 07
166 ├"*"
167 AIP
168▸LBL 04
169 AVIEW
170 END
PFCT.raw
(315 Bytes) Downloaded 163 times
I also tried to upload the small Prho.raw file without success for some reason,....
/Hakan

HP21, HP32SII,HP42S, HP48 SX,G,GX, DM42
User avatar
rudi
Posts: 415
Joined: Wed Nov 03, 2021 9:03 am
Location: Denmark
Contact:

Re: A “built-in” primality test function and composite numbers factorisation

Post by rudi »

Very nice work. Primefactor function is something I miss in the WP42/C43/C47 series, where the only prime number function that exists is NEXTP (which by the way is extremely fast)
/Rudi

DM-42 (s/n 06999), HP-42S, HP-35s, HP-11c, HP-32SII (ex HP-41CV, ex HP-75C, ex HP-48G + a lot, really lot of a accessories)
Denmark
Hawk45
Posts: 7
Joined: Fri Feb 17, 2023 2:45 pm
Location: Stockholm, Sweden

Re: A “built-in” primality test function and composite numbers factorisation

Post by Hawk45 »

The prime factorisation journey continues with another big improvement in execution times on battery power. In the attached function “BRENT”, I have implemented the Pollard/Brent Rho algorithm. Compared to Pollard/Floyd Rho in function “Prho”, BRENT is much faster.

The example in my previous post where the number 1E17 - 45 took 4 min 18 sec with Prho now executes in 52 sec with BRENT. With 7-9 digits in the second largest prime factor in a number N, BRENT usually takes 5-30 sec instead of 30 sec - 2 min for Prho.
Occasionally it will need 40-50 sec to split a number, and it can even take longer: The number 90,342,523^2 takes 1 min 17 sec; so far the only number I have found that exceeds 1 min to split.

The attached PDF contains source code in Python for the Pollard/Floyd and Pollard/Brent algorithms. I basically ported this Python code to DM42 with some changes and adaptations to the stack environment.

To install the BRENT function, just replace XEQ "Prho" with XEQ "BRENT" in the PFCT progran.

Code: Select all

00 { 213-Byte Prgm }
01▸LBL "BRENT"
02 FUNC 01
03 LSTO "N"
04 200
05 LSTO "M"
06 2
07 LSTO "Y"
08 CLX
09 LSTO "X"
10 LSTO "K"
11 LSTO "YS"
12 SIGN
13 LSTO "R"
14 LSTO "Q"
15▸LBL 02
16 RCL+ "R"
17 RCL "N"
18 RCL "Y"
19 STO "X"
20▸LBL 03
21 X↑2
22 1
23 +
24 RCL ST Y
25 MOD
26 DSE ST Z
27 GTO 03
28 STO "Y"
29 CLX
30 STO "K"
31▸LBL 04
32 RCL "R"
33 RCL- "K"
34 RCL "M"
35 X>Y?
36 X<>Y
37 1
38 +
39 RCL "Q"
40 RCL "Y"
41 STO "YS"
42▸LBL 05
43 X↑2
44 1
45 +
46 RCL "N"
47 MOD
48 ENTER
49 RCL- "X"
50 ABS
51 X<>Y
52 R↓
53 ×
54 RCL "N"
55 MOD
56 R↑
57 DSE ST Z
58 GTO 05
59 STO "Y"
60 X<>Y
61 STO "Q"
62 LASTX
63▸LBL 00
64 MOD
65 LASTX
66 X<>Y
67 X≠0?
68 GTO 00
69 X<>Y
70 1
71 X≠Y?
72 GTO 06
73 RCL "M"
74 STO+ "K"
75 RCL "R"
76 RCL "K"
77 X<Y?
78 GTO 04
79 2
80 STO× "R"
81 SIGN
82 GTO 02
83▸LBL 06
84 RCL "N"
85 LASTX
86 X≠Y?
87 RTN
88 RCL "YS"
89 1
90 ENTER
91▸LBL 10
92 R↓
93 X<>Y
94 X↑2
95 +
96 RCL ST Y
97 MOD
98 ENTER
99 RCL- "X"
100 ABS
101 RCL ST Z
102▸LBL 01
103 MOD
104 LASTX
105 X<>Y
106 X≠0?
107 GTO 01
108 SIGN
109 X=Y?
110 GTO 10
111 X<>Y
112 END
Pollard.pdf
(34.98 KiB) Downloaded 166 times
BRENT.raw
(216 Bytes) Downloaded 162 times
/Hakan

HP21, HP32SII,HP42S, HP48 SX,G,GX, DM42
Hawk45
Posts: 7
Joined: Fri Feb 17, 2023 2:45 pm
Location: Stockholm, Sweden

Re: A “built-in” primality test function and composite numbers factorisation

Post by Hawk45 »

I have remowed a small “blemish” on the PFCT program, namely that it outputs composite factors with a question mark when Pollard Rho fails. It now splits such factors and always outputs all prime factors in the number tested. The example in a previous post with the number 50,428,999,949,571 now looks like this:
PFCT_ok.bmp
PFCT_ok.bmp (12.31 KiB) Viewed 2432 times
Two small changes are needed for this to work:
  • In Prho the c in the polynom x^2 + c now is a random digit 1-9 instead of beeing a constant = 1
  • PFCT calls Prho (repeatedly if necessary) to split a composite factor whenever BRENT fails
I have also removed the factorisation wheel from PFCT as it is no longer needed with Pollard Rho.

In summary, the Prime Factorisation “suite” now consits of:
  • PFCT 184 bytes
  • BRENT 213 bytes
  • Prho 75 bytes
  • PRM? 192 bytes
Note: PFCT calls the other 3 programs.

Code: Select all

00▸{ 184-Byte Prgm }
01 LBL "PFCT"
02 FUNC 00
03 REAL?
04 SKIP
05 RTNERR 4
06 ENTER
07 ABS
08 "Prime"
09 XEQ "PRM?"
10 GTO 04
11 CLA
12 X≠Y?
13 "-"
14 GTO 00
15▸LBL 20
16 X<>Y
17 ENTER
18 ISG ST Z
19 NOP
20 ENTER
21 ANUM
22 X≠Y?
23 ├"*"
24 R↓
25 LASTX
26 AIP
27 ÷
28 STO ST Y
29 LASTX
30 MOD
31 X≠0?
32 RTN
33 ├"↑"
34▸LBL 02
35 X<>Y
36 STO ST Y
37 LASTX
38 ÷
39 ISG ST Z
40 NOP
41 STO ST Y
42 LASTX
43 MOD
44 X=0?
45 GTO 02
46 X<>Y
47 R↑
48 AIP
49 RTN
50▸LBL 00
51 STO ST Y
52 2
53 MOD
54 X=0?
55 XEQ 20
56 X<>Y
57 STO ST Y
58 3
59 MOD
60 X=0?
61 XEQ 20
62 X<>Y
63 STO ST Y
64 5
65 MOD
66 X=0?
67 XEQ 20
68 X<>Y
69 STO ST Y
70 7
71 MOD
72 X=0?
73 XEQ 20
74 X<>Y
75 1
76 X=Y?
77 GTO 04
78▸LBL 06
79 X<>Y
80 ENTER
81 XEQ "PRM?"
82 GTO 09
83 XEQ "BRENT"
84▸LBL 08
85 XEQ "PRM?"
86 GTO 09
87 R↓
88 XEQ "Prho"
89 GTO 08
90▸LBL 09
91 STO ST L
92 CLX
93 XEQ 20
94 SIGN
95 X≠Y?
96 GTO 06
97▸LBL 04
98 AVIEW
99 END

Code: Select all

00▸{ 75-Byte Prgm }
01 LBL "Prho"
02 FUNC 01
03 LSTO "N"
04 RAN
05 10↑X
06 IP
07 1
08 X<Y?
09 X<>Y
10 LSTO "C"
11 2
12 ENTER
13 GTO 00
14▸LBL 10
15 X↑2
16 RCL+ "C"
17 RCL "N"
18 MOD
19 RTN
20▸LBL 02
21 R↓
22 R↓
23 X<>Y
24▸LBL 00
25 XEQ 10
26 X<>Y
27 XEQ 10
28 XEQ 10
29 DUPN 2
30 -
31 ABS
32 RCL "N"
33▸LBL 01
34 MOD
35 LASTX
36 X<>Y
37 X≠0?
38 GTO 01
39 SIGN
40 X=Y?
41 GTO 02
42 X<>Y
43 END
PFCT.raw
(187 Bytes) Downloaded 162 times
Prho.raw
(78 Bytes) Downloaded 170 times
/Hakan

HP21, HP32SII,HP42S, HP48 SX,G,GX, DM42
Post Reply