Here's my version of the program (long) Message #15 Posted by Axel Poqué on 28 July 2003, 1:05 p.m., in response to message #13 by W. Bruce Maguire II
Ok, here's my version of the program. I've added a few lines to time the program (lines 06-08, 56 and 58-60), and also the possibility to provide a maximum number to test (say, you want all primes up to 1000).
If we remove the special case of '2' as a prime (by starting at 3 instead of 1), we can reduce the running time of the program by about 25% since we only need to test uneven numbers in this case.
To run it, you put the maximum number to test on the stack, and do an [XEQ] "PRIME1".
01 LBL "PRIME1"
02 STO 06 ; last number to test
03 FIX 00 ; set display to show integers
04 CF 29 ; no decimal point
05 0
06 STOPSW ; start stopwatch (remove lines 06-08 if you
07 SETSW ; don't want to use the stopwatch)
08 RUNSW
10 STO 07
11 3
12 STO 01
13 LBL 00
14 2
15 STO 03
16 1
17 STO 04
18 RCL 01
19 SQRT
20 STO 02
21 LBL 02
22 RCL 02
23 RCL 03
24 X>Y?
25 GTO 05
26 RCL 01
27 RCL 03
28 /
29 FRC
30 X=0?
31 GTO 04
32 RCL 03
33 4
34 X<=Y?
35 GTO 03
36 2
37 ST* 03
38 1
39 ST- 03
40 GTO 02
41 LBL 03
42 3
43 ST+ 03
44 RCL 04
45 CHS
46 STO 04
47 ST+ 03
48 GTO 02
49 LBL 04
50 2 ; only uneven numbers (3, 5, 7, ...)
51 ST+ 01
52 RCL 06 ; loop if not maximum number
53 RCL 01
54 X<=Y?
55 GTO 00
56 STOPSW ; remove if no timing needed
57 SF 29 ; reset decimal flag
58 FIX 06 ; 6 decimal places to show time
59 RCLSW ; show expired time
60 VIEW X ; lines 58-60 can be removed if you
; don't want to time the program
61 STOP
62 LBL 05 ; show prime number
63 CLA
64 ARCL 01
65 AVIEW
66 1 ; count primes
67 ST+ 07
68 GTO 04
69 END
I haven't been able to test it on a real HP-41, since I sent mine back for replacement (it suffered from frequent memory losses, if you remember). I ran it in V41 though, where finding all primes up to 100 takes 41 s instead of 56 s.
Axel
Edited: 28 July 2003, 1:09 p.m.
|