In my previous post I described some logic that could be used to test whether a number is prime. The first bit of the psuedo code checks to see if the number entered, X, is greater than or equal to 372 = 1369. This is the range of this code but it can be altered easily to check numbers much larger than this. It depends on how much memory you have available in your fx-50F calculator. For example, by continuing the test division of X by eight more primes 37, 41, 43, 47, 53, 59, 61 and 67, it would be possible to test numbers up to but not including 712 = 5041.
The next part of the code checks to see if X is less than 11 and, if it is, checks to see if it is one of the primes 2, 3, 5, or 7. If it is Y is set to 1 otherwise Y is 0.
If X is greater than or equal to 11 we can divide X by each of the prime numbers 2, 3, 5, and 7 to see if it is divisible by any of these numbers. As described previously, we use the calculator's sine function for this purpose. Again, if X is divisible by one of these numbers, then Y is set to 0 (i.e. X is not prime) otherwise it is set to 1. If X is less than 121 (=112) then we need not test the number any further.
If X is greater than or equal to 121 we can also try dividing it by the prime numbers 11, 13, 17 and 19. Again, if X is divisible by one of these numbers, then Y is set to 0 (i.e. X is not prime) otherwise it is set to 1. If X is less than 529 (=232) then we need not test the number any further.
Effectively we are bootstraping our way up through the prime numbers and the process is repeated in the remaining steps. As such, it can be extended, within the limits of the calculator, to much larger numbers as described above.
Tuesday, 8 May 2012
Wednesday, 2 May 2012
Is it Prime - the logic
Previously I described how to go about creating an algorithm that could test whether a number is prime. Here is some suitable logic for the fx-50F:-
Label 0
Input X (the number to be tested)
If X is greater than or equal to 37 squared then
Store 999 in Y (this indicates that X is out of range)
Goto Label 2
EndIf
If X is greater than or equal to 11 then Goto Label 1
Store 0 in Y (Y is 1 if X is prime, 0 if X is not prime)
If X=2 then store 1 in Y
If X=3 then store 1 in Y
If X=5 then store 1 in Y
If X=7 then store 1 in Y
Goto Label 2
Label 1
Store πX in A
Store 1 in Y
If sin(A/2)=0 then store 0 in Y
If sin(A/3)=0 then store 0 in Y
If sin(A/5)=0 then store 0 in Y
If sin(A/7)=0 then store 0 in Y
If Y=0 then Goto Label 2
If X is less than 121 then Goto Label 2
If sin(A/11)=0 then store 0 in Y
If sin(A/13)=0 then store 0 in Y
If sin(A/17)=0 then store 0 in Y
If sin(A/19)=0 then store 0 in Y
If Y=0 then Goto Label 2
If X is less than 529 then Goto Label 2
If sin(A/23)=0 then store 0 in Y
If sin(A/29)=0 then store 0 in Y
If sin(A/31)=0 then store 0 in Y
Label 2
Display Y
Goto Label 0
Label 0
Input X (the number to be tested)
If X is greater than or equal to 37 squared then
Store 999 in Y (this indicates that X is out of range)
Goto Label 2
EndIf
If X is greater than or equal to 11 then Goto Label 1
Store 0 in Y (Y is 1 if X is prime, 0 if X is not prime)
If X=2 then store 1 in Y
If X=3 then store 1 in Y
If X=5 then store 1 in Y
If X=7 then store 1 in Y
Goto Label 2
Label 1
Store πX in A
Store 1 in Y
If sin(A/2)=0 then store 0 in Y
If sin(A/3)=0 then store 0 in Y
If sin(A/5)=0 then store 0 in Y
If sin(A/7)=0 then store 0 in Y
If Y=0 then Goto Label 2
If X is less than 121 then Goto Label 2
If sin(A/11)=0 then store 0 in Y
If sin(A/13)=0 then store 0 in Y
If sin(A/17)=0 then store 0 in Y
If sin(A/19)=0 then store 0 in Y
If Y=0 then Goto Label 2
If X is less than 529 then Goto Label 2
If sin(A/23)=0 then store 0 in Y
If sin(A/29)=0 then store 0 in Y
If sin(A/31)=0 then store 0 in Y
Label 2
Display Y
Goto Label 0
Subscribe to:
Posts (Atom)