Post Reply 
RPL exercise - Last Digits of Primes (HP 49G, G+, 50g)
06-01-2019, 11:05 PM (This post was last modified: 06-01-2019 11:06 PM by Dave Britten.)
Post: #17
RE: RPL exercise - Last Digits of Primes (HP 49G, G+, 50g)
I don't have my 48 handy at the moment, but here's a 42S/DM42 version. Input the number of primes greater than 5 to look for, and XEQ "PRMDGT". The number of primes ending in 1, 3, 7, and 9 will be in stack T, Z, Y, and X respectively. This uses the well-known mod 30 sieve to check for primes.

Code:
00 { 53-Byte Prgm }
01▸LBL "PRMDGT"
02 CLRG
03 STO 06
04 5
05 STO 05
06▸LBL 00
07 2
08 STO+ 05
09▸LBL 01
10 RCL 05
11 XEQ "PRIME?"
12 FC? 01
13 GTO 00
14 RCL 05
15 10
16 MOD
17 1
18 STO+ IND ST Y
19 DSE 06
20 GTO 00
21 RCL 01
22 RCL 03
23 RCL 07
24 RCL 09
25 CF 01
26 END

00 { 96-Byte Prgm }
01▸LBL "PRIME?"
02 STO 00
03 SF 01
04 0
05 STO 02
06 2
07 XEQ 02
08 1
09 XEQ 02
10 2
11 XEQ 02
12 2
13 XEQ 02
14▸LBL 01
15 RCL 00
16 RCL 02
17 X↑2
18 X>Y?
19 RTN
20 4
21 XEQ 02
22 2
23 XEQ 02
24 4
25 XEQ 02
26 2
27 XEQ 02
28 4
29 XEQ 02
30 6
31 XEQ 02
32 2
33 XEQ 02
34 6
35 XEQ 02
36 GTO 01
37▸LBL 02
38 STO+ 02
39 RCL 00
40 RCL 02
41 X=Y?
42 RTN
43 MOD
44 X=0?
45 CF 01
46 RTN
47 END

This finds and tallies 1000 primes in about 25 seconds on my DM42 when running in fast mode (i.e. connected to external power).
Visit this user's website Find all posts by this user
Quote this message in a reply
Post Reply 


Messages In This Thread
RE: RPL exercise - Last Digits of Primes (HP 49G, G+, 50g) - Dave Britten - 06-01-2019 11:05 PM



User(s) browsing this thread: