10REM > Primes
20REM Various Prime Number Programs
30REM Header by J.G.Harston W79
40:
50ONERROR REPORT:PRINT'"Press a key";
GET:RUN
60MODE7:PRINTSPC7"Prime Number Genera
tion"
70PRINT'"Press a key:"'
80PRINT"1: R.Harker's original. Test
s all"'SPC4"numbers with all numbers"
90PRINT"2: Peter Davy's improvement.
Tests and"'SPC4"uses only odd numbers u
p to SQR(x)"
100PRINT"3: J.G.Harston's improvement.
Only uses"SPC4"6n-1, 6n+1 up to SQR(x)
"
110PRINT"4: J.G.Harston's fast integer
primes"
120PRINT"5: J.G.Harston's quadratic in
teger"'SPC4"primes using y=x^2+x+41"
130PRINT"6: J.G.Harston's IsPrime func
tion"
140PRINT'"0: Exit"
150REPEATA$=GET$:UNTILINSTR("0123456",
A$):IFA$="0":END
160A$="Primes"+A$:PRINT'A$;
170A%=PAGE:REPEAT:A%=A%+4+LEN$A%:UNTIL
(INSTR($A%,CHR$&F4)>0 AND INSTR($A%,A$)>
0) OR A%>TOP:IFA%>TOP:END
180Line%=256*A%?-3+A%?-2:PRINTLine%,÷L
ine%:GOTOLine%
190:
200DEFPROCSplit:S%=0:A$="":A%=PAGE+4+L
EN$(PAGE+4):REPEAT
210REPEATA%=A%+4+LEN$A%
220UNTIL(INSTR($A%,CHR$&F4)>0 AND INST
R($A%,">")>1) OR A%>TOP
230IFS% AND A$<>"":M%=A%?-3:A%?-3=&FF:
OSCLI"SAVE "+A$+" "+STR$÷S%+" "+STR$÷(A%
-2)+" FFFF0000 FFFFFB00":A%?-3=M%
240S%=A%-4:A$=MID$($A%,1+INSTR($A%,">"
)):UNTILA%>TOP:END
250:
260A%=0:*KEY 0 A%=A%+1:OS."LOAD Primes
"+STR$(A%)+" "+STR$÷(TOP-2):END|MREN.|M
270:
280:
290:
300REM > Primes1
310REM D6E (R. Harker)'s original vers
ion
320MODE0
330 place=1
340 divisable=0
350 number=0
360 T%=TIME:REPEAT
370 FOR divisable=1 TO number
380 IF (number MOD divisable)=0 TH
EN a=a+1
390 IF a>2 THEN divisable=number
400 IF (number MOD 2=0 AND number>
2) THEN divisable=number
410 NEXT divisable
420 IF a=2 THEN PRINT number;" Is pr
ime number ";place;" after ";(TIME-T%)/1
00;" secs"
430 IF a=2 THEN place=place+1
440 a=0
450 number=number+1
460 UNTIL FALSE
470:
480:
490:
500REM > Primes2
510REM K2K (Peter Davy)'s modification
of
520REM D6E (R. Harker)'s Prime Number
program
530MODE0
540 PRINT 2;" is prime number ";1:PRIN
T 3;" is prime number ";2
550 number%=5
560 try%=1
570 order%=2
580 T%=TIME:REPEAT
590 REPEAT
600 try%=try%+2
610 IF (number% MOD try%)=0 THEN facto
rfound=TRUE ELSE factorfound=FALSE
620 UNTIL factorfound OR try%>SQR(numb
er%)
630 IF NOT factorfound AND try%>SQR(nu
mber%) THEN order%=order%+1:PRINT number
%;" is prime number ";order%;" after ";(
TIME-T%)/100;" secs"
640 number%=number%+2:try%=1
650 UNTIL FALSE
660:
670:
680:
690:
700REM > Primes3
710MODE0
720REM W79 (J.G.Harston)'s modificatio
n to
730REM K2K (Peter Davy)'s modification
to
740REM D6E (R. Harker)'s Primes progra
m
750:
760 PRINT 2;" is prime number ";1:PRIN
T 3;" is prime number ";2
770 number%=5:number`inc%=2
780 order%=2
790 T%=TIME:REPEAT
800 try%=1:try`inc%=4
810 factorfound=(number% MOD 2)=0 OR (
number% MOD 3)=0
820 IF NOT factorfound THEN REPEAT:try
%=try%+try`inc%:try`inc%=6-try`inc%:fact
orfound=(number% MOD try%)=0:UNTIL facto
rfound OR try%>SQR(number%)
830 IF NOT (factorfound AND try%<=SQR(
number%)) order%=order%+1:PRINT number%;
" is prime number ";order%;" after ";(TI
ME-T%)/100;" secs"
840 number%=number%+number`inc%:number
`inc%=6-number`inc%
850 UNTIL FALSE
860:
870:
880:
890:
900REM > Primes4
910REM 21-09-96 JGH: Fast 6n-1, 6n+1 p
rime lister
920:
930MODE128:PRINT2;" is prime number 1"
:PRINT3;" is prime number 2"
940N%=5:I%=2:O%=2:T%=TIME:REPEAT
950D%=1:A%=4:F%=FALSE:IF(N%MOD3):REPEA
TD%=D%+A%:A%=6-A%:F%=(N%MODD%)=0:UNTILF%
OR D%>=SQRN%
960IFNOT(F% AND D%<=SQRN%):O%=O%+1:PRI
NTN%;" is prime number ";O%;" after ";(T
IME-T%)/100;" secs"
970N%=N%+I%:I%=6-I%:UNTILFALSE
980:
990:
1000REM > Primes5
1010REM 19-09-96 JGH: Fast quadratic co
mbination
1020:
1030MODE128:O%=3
1040PRINT2;" is prime 1":PRINT3;" is pr
ime 2"
1050N%=5:I%=2:O%=2:T%=TIME:REPEAT
1060F%=TRUE:IFN%>42 AND N%<1602:N=(SQR(
4*N%-163)-1)/2:F%=N<>INTN
1070F%=F%OR((N%MOD3)=0):D%=1:J%=4:IFF%:
REPEAT D%=D%+J%:J%=6-J%:F%=(N% MOD D%)=0
:UNTILF% OR D%>=SQR(N%)
1080IFNOT(F% AND D%<=SQR(N%)):O%=O%+1:P
RINTN%;" is prime ";O%;" after ";(TIME-T
%)/100;" secs"
1090N%=N%+I%:I%=6-I%:UNTILFALSE
1100:
1110:
1120:
1130:
1140:
1150REM > Primes6
1160REM 21-09-96 JGH: Function to test
for primeness
1170:
1180MODE0:N%=5:I%=2:O%=2:T%=TIME:REPEAT
1190IFFNIsPrime(N%):O%=O%+1:PRINTN%;" i
s prime ";O%;" after ";(TIME-T%)/100;" s
ecs"
1200N%=N%+I%:I%=6-I%:UNTILFALSE
1210:
1220DEFFNIsPrime(N%):LOCAL N,D%,K%,F%:I
F(N%MOD2)=0 OR (N%MOD3)=0:=FALSE
1230IFN%>42 AND N%<1602:N=(SQR(4*N%-163
)-1)/2:IFN=INTN:=TRUE
1240D%=1:J%=4:REPEAT D%=D%+J%:J%=6-J%:F
%=(N% MOD D%)=0:UNTILF% OR D%>=SQR(N%):=
NOT(F% AND D%<=SQR(N%))
1250:
1260: