(12C+) how many primes between two numbers
06-30-2019, 11:30 PM (This post was last modified: 06-30-2019 11:41 PM by Don Shepherd.)
Post: #1
 Don Shepherd Senior Member Posts: 739 Joined: Dec 2013
(12C+) how many primes between two numbers
Here is a cute little program for the 12c+ that will tell you the number of prime numbers between two numbers you specify. It takes a rather brute force approach, which is why I recommend it for the 12c+ and not the slower 12c.

It also uses the N financial register as a flag. If you set N=0, it will just tell you the number of primes between the two numbers you specify. If you set N to anything else, it will pause and show you (briefly) each prime that it finds, and give you the total primes at the end. I think this is a neat and easy way to use N as a flag.

To use the program, set N to 0 or non-zero depending upon whether you want to see each prime, then enter your range in X and Y (lower point in Y, upper point in X), then press R/S and be amazed. It works best if your range is under 1000 or 2000, because although the 12c+ is fast, it is still brute force.

Code:
 01 sto 2 02 x<->y 03 sto 1 04 0 05 sto 3 06 rcl 1 07 2 08 / 09 frac 10 x=0 11 goto 13 12 goto 15 13 1 14 sto+1 15 3 16 sto 0 17 rcl 1 18 sqrt 19 sto 6 20 rcl 1 21 rcl 0 22 / 23 frac 24 x=0 25 goto 39 26 2 27 sto+0 28 rcl 6 29 rcl 0 30 x<=y 31 goto 20 32 rcl n 33 x=0 34 goto 37 35 rcl 1 36 pse 37 1 38 sto+3 39 2 40 sto+1 41 rcl 2 42 rcl 1 43 x<=y 44 goto 15 45 rcl 3
08-22-2019, 06:48 AM (This post was last modified: 08-22-2019 07:40 AM by Gamo.)
Post: #2
 Gamo Senior Member Posts: 674 Joined: Dec 2016
RE: (12C+) how many primes between two numbers
Very nice program tested and work great !!

I just happen to see the example program to
"Generate Prime Number" from the HP-35S User Manual and I use that algorithm to
work on the HP-12C+ or Emulators.

I make some modification, instead of testing against square root I use X^2
this will run faster for large number.

Procedure:

If the input number is already prime the same number is display if not the next
prime larger than the previous number display.

FIX 0

n [R/S] display Prime Number [R/S] next Prime Number

Program:
Code:
 01 STO 0  // Store integer value 02  3 03 STO 1  // Store 3 to start the test divisor 04 RCL 0 05  2 06  ÷ 07 FRAC 08 X=0  // Tests for even number input 09 GTO 25  // This integer is an even number go ahead and branches to line 25  10 RCL 0 11 RCL 1 12  ÷ 13 FRAC  14 X=0  // Test for a remainder of zero (not prime) 15 GTO 27  // Not prime tries next possibility  16 RCL 1 17 ENTER 18  x 19 RCL 0 20 X≤Y  // Test to see whether all possible factors have been tried 21 GTO 30  // If all have been tried, branches to line 30 to display prime number 22  2 23 STO+1  // Increment the divisor   24 GTO 10 25  1  // Increment integer if input an even number 26 GTO 28 27  2  // Increment integer to search for a prime number 28 STO+0 29 GTO 02 30 RCL 0  // Finally got the precious prime number 31 R/S  // Stop and add one to previous prime number 32  1 33  + 34  GTO 01  // Start over to generate the next prime number

Remark: Is there a better algorithm than this to search faster?

Gamo
 « Next Oldest | Next Newest »

User(s) browsing this thread: 1 Guest(s)