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

No comments:

Post a Comment