; *************************************************************
; Prime Number Checker
; *************************************************************
; Prime Number Testing, Sourcebook for Programmable Calculators
;  (Pages 1-9 thru 1-11)

#EMULATOR c:\ti-59\prg2\prime\prime.t59
#LISTING c:\ti-59\prg2\prime\prime.lst
#POCKETPC c:\ti-59\prg2\prime\prime.ti

; Stores N and checks to see if N<=1.  If N<=1, program transfers
; to E.  Divides N by 2 and checks for remainder.  If remainder=0,
; transfers to D.  Checks to see if N<=3; if so, transfers to D.
; Stores 3 and sqrt(N) for later testing.
; ----------------------------------------------------------------
lbl a
sto 1 x%t 1 ge e 3 ge c rcl 1
/ 2 = inv int x%t 0 eq d 3 sto 2
rcl 1 sqr sto 3

; Divides N by 3 and checks for remainder.  If remainder=0, transfers
; to D.  If remainder<>0, increases divisor by 2 and tests against
; sqrt(N).  If divisor<N, loops back to B.
; ----------------------------------------------------------------
lbl b
rcl 1 / rcl 2 = inv int x%t 0
eq d 2 sum 2 rcl 2 x%t rcl 3 ge b

; N is prime. Recals N and stops.
; -------------------------------
lbl c
"PRIME line>> rcl 1 mixline r/s

; N is not prime.  Recalls flashing N and stops.
; ----------------------------------------------
lbl d
rcl 1 x2 +/- sqr r/s

; N<=1 (not prime by definition).  Clears display and stops.
; ----------------------------------------------------------
lbl e 
0 r/s
 



 
#end
