Thursday 25 October 2012

Is it prime - execution of the program

In a previous post I provided a program for determining whether a number less than 1369 is prime. To show how this works consider the number 431. On running the program the display shows X? This is the number we want to test, so enter 431 and press [EXE]. The display shows Y is 1 indicating that 431 is a prime number (which is the case). Now press [EXE] again and enter 1233 (which is obviously divisible by 3) when the display shows X? On pressing [EXE] the display now shows Y is 0 indicating that 1233 is not prime. Press [EXE] once more and enter 2000 followed by [EXE]. The display now shows Y is 999 and this indicates that the number entered is out of range.

I notice that I haven't included a check to see if the number entered is less than or equal to zero (which would be out of range too) but I leave this as an exercise for the student!

Wednesday 24 October 2012

Is it prime - the program

I discussed the logic for this program in a previous blog. Here is the logic converted into a program for the fx-50F:-


Lbl 0:?→X:If X≥37²:Then 999→Y:Goto 2:IfEnd:X≥11=>Goto 1:
0→Y:X=2=>1→Y:X=3=>1→Y:X=5=>1→Y:X=7=>1→Y:Goto 2:Lbl 1:πX→A:
1→Y:sin(A÷2)=0=>0→Y:sin(A÷3)=0=>0→Y:sin(A÷5)=0=>0→Y:
sin(A÷7)=0=>0→Y:Y=0=>Goto 2:X<121>Goto2:
sin(A÷11)=0=>0→Y:sin(A÷13)=0=>0→Y:sin(A÷17)=0=>0→Y:
sin(A÷19)=0=>0→Y:Y=0=>Goto 2:X<329>Goto2:
sin(A÷23)=0=>0→Y:sin(A÷29)=0=>0→Y:sin(A÷31)=0=>0→Y:
Lbl 2:Y▲Goto 0:

This is pretty much how the program looks when keyed into the calculator in edit mode. Special Program Commands (? → => : Lbl = < ≥ If Then IfEnd ▲ Goto) are input using the [SHIFT] [P-CMD] keys. The cursor key (marked Replay) is used to switch between various screens of commands. Memory variables A, X and Y are input using [ALPHA] [A], [X] etc keys. The ":" Separator Code command is used frequently and can be alternatively input using the [EXE] key.

This program is 264 steps in length.