8-Bit Software Online Conversion

Prime Numbers - Listing

10REM" P R I M A X ? 20REM Four prime number programs. 30REM John Davis - '96. 40MODE7:VDU23,1,0;0;0;0; 50LIN$=CHR$(145)+STRING$(39,",") 60PM$=" * P R I M A X ? * " 70DIMB%(48):DIMT%(300):DIMU%(300) 80FORN%=1TO48:READA%:B%(N%)=A%:NEXT 90DATA10,2,4,2,4,6,2,6,4,2,4,6,6,2,6, 4,2,6,4,6,8,4,2,4,2,4,8,6 100DATA4,6,2,4,6,2,6,6,4,2,4,6,2,6,4,2 ,4,2,10,2 110PRINTPM$ 120PRINT'" This is another collectio n of three little programs, following on from PRIMATE in 8BS-52, trying t o squeeze out a bit more speed genera ting prime numbers by trial division." 130PRINT" Here, the timings to the 1 000th prime are: Program 1 - 1min11.7sec ; Program 2 - 1min2.4sec; and, aha!, P rogram 3 - 55.1sec. One interesting fac tor that helps the speed is allowing unnecessary"; 140PRINT" calculations in order to red uce the number of other calculations ." 150PRINT" For structural reasons, th ese three programs all start at the 7t h prime, so, to avoid just printing t he first six, they are generated by a separate - but slow - routine." 160PRINTLIN$; 170PRINT" ESCAPE at any time gives r unning time and Repeat/Menu/Escape optio ns." 180PRINTLIN$; 190PRINTSPC(17);"Press SPACE for Menu. "; 200G=GET:CLS 210ONERRORVDU23,1,1;0;0;0;:END 220VDU15:PRINTPM$ 230PRINT'TAB(14)" Menu " 240PRINT" Program:" 250PRINTCHR$(145)STRING$(8,",") 260PRINT" 1) Self-contained one-line r outine, skipping multiples of 3 i n test numbers and divisors." 270PRINT" Note that this program ca n Continue."; 280PRINT" Time to 1000th prime: 1mi n11.7sec." 290PRINT'" 2) Array skipping multiples of 3, 5, and 7 in test numbers." 300PRINT" Time to 1000th prime: 1mi n2.4sec." 310PRINT'" 3) Array as 2), + 12 fixed divisors." 320PRINT" Time to 1000th prime: 55. 1sec." 330PRINT'" 4) Fermat. This is the same as the Fermat's Little Theorem program in 8BS-52, but tidied up an d quicker." 340PRINT'" ESC To Escape." 350PRINTLIN$; 360G$=GET$ 370ONERROR IFERR=17 CLS:VDU23,1,0;0;0; 0;:GOTO210 ELSEPRINT"ERROR at line ";ERL :END 380IF G$="1" THEN450 390IF G$="2" THEN510 400IF G$="3" THEN560 410IF G$="4" THEN700 420ONERRORVDU23,1,1;0;0;0;:END 430GOTO360 440REM"  450ONERRORT%=TIME:GOTO1260 460REM" Program 1 470CLS:F%=1:X%=9:TIME=0 480PROCfirstsix 490FORA%=X%TO2^31-1STEP4:Z%=TIME:X%=A% :Y%=C%:S%=SQR(A%+202):FORA%=A%+2TOA%+200 STEP2:FORA%=A%TOA%+2STEP2:FORN%=5TOS%STE P6:IFA%MODN%IFA%MOD(N%+2)NEXT:PRINT" ";A %;" is prime ";C%:C%=C%+1:NEXT:NEXT:NEXT ELSEN%=S%:NEXT:NEXT:NEXT:NEXT 500REM"  510ONERRORT%=TIME:GOTO1260 520REM" Program 2 530CLS:F%=2:TIME=0:PROCfirstsix 540A%=1:FORJ%=1TO1STEP0:S%=SQR(A%+210) :FORM%=1TO48:A%=A%+B%(M%):FORN%=11TOS%ST EP6:IFA%MODN%IFA%MOD(N%+2)NEXT:PRINT" "; A%" is prime ";C%:C%=C%+1:NEXT:NEXTELSEN %=S%:NEXT:NEXT:NEXT 550REM"  560ONERRORT%=TIME:GOTO1260 570REM" Program 3 580CLS:F%=3:TIME=0:PROCfirstsix 590REM" To 399th prime. 600A%=1:FORJ%=1TO13:S%=SQR(A%+210):FOR M%=1TO48:A%=A%+B%(M%):FORN%=11TOS%STEP6: IFA%MODN%IFA%MOD(N%+2)NEXT:PRINT" ";A%" is prime ";C%:C%=C%+1:NEXT:NEXTELSEN%=S% :NEXT:NEXT:NEXT 610REM" From 400th prime. 620FORJ%=1TO1STEP0:S%=SQR(A%+210):FORM %=1TO48:A%=A%+B%(M%):IFA%MOD11IFA%MOD13I FA%MOD17IFA%MOD19IFA%MOD23IFA%MOD29IFA%M OD31IFA%MOD37IFA%MOD41IFA%MOD43IFA%MOD47 IFA%MOD53ELSENEXT:NEXT 630FORN%=59TOS%STEP6:IFA%MODN%IFA%MOD( N%+2)NEXT:PRINT" ";A%" is prime ";C%:C%= C%+1:NEXT:NEXTELSEN%=S%:NEXT:NEXT:NEXT 640REM"  650DEFPROCfirstsix 660A%=1:FORC%=1TO6:FORJ%=1TO1STEP0:A%= A%+1+(1ANDA%>2):S%=SQR(A%):FORN%=3TOS%ST EP2:IFA%MODN%ORN%>S%NEXT:PRINT" ";A%;" i s prime ";C%:J%=2:NEXT:NEXTELSEN%=S%:NEX T:NEXT 670ENDPROC 680REM"  690REM" Program 4 700AA%=2 710ONERRORVDU23,1,0;0;0;0;:CLS:GOTO210 720CLS:PRINT" * F E R M A T * " 730PRINT'" This program tests all odd numbers, to see which produce '1', incl uding all odd non-primes, using Ferma t's Little Theorem. For more info, see 8BS-52." 740PRINTLIN$; 750PRINT'" As the program runs, the no n-primes are displayed automatically. Th ese are stored, and a list can be c alled at any time by pressing 'N'. The p rogram then continues." 760PRINT" Pressing Space returns here. " 770PRINTLIN$ 780PRINT" Base A is currently ";AA% 790PRINT'" To run with this, just pres s Return, or enter a new number." 800PRINTTAB(0,23);LIN$; 810VDU23,1,1;0;0;0; 820PRINTTAB(0,21);:INPUT" Base A ? "Z% 830IFZ%<>0 AA%=Z% 840ONERRORT%=TIME:GOTO1260 850VDU23,1,0;0;0;0;:CLS:F%=4:TIME=0 860P$=" is prime ":PRINT" ";2P$;1:PRIN T" ";3P$;2:PRINT" ";5P$;3:PRINT" ";7P$;4 :PRINT" ";11P$;5 870REM" Fermat routine 880C%=5:K%=0 890FORP%=11TO46339STEP2 900Q%=P%-1:A%=AA%:X%=1 910REPEAT 920IFQ%MOD2=1X%=(X%*A%)MODP% 930Q%=Q%DIV2 940A%=A%*A% 950IFA%>P%A%=A%MODP% 960UNTILQ%=1 970R%=(A%*X%)MODP% 980IF R%=1 C%=C%+1:PRINT" ";P%;" is pr ime ";C%:GOTO1030 990NEXT 1000REM" * * * * 1010GOTO1200 1020REM" Check 1030G$=INKEY$(0) 1040IFG$="N" PROCprint 1050IFG$=" " THEN710 1060IFP%MOD3S%=SQR(P%):FORN%=5TOS%STEP6 :IFP%MODN%IFP%MOD(N%+2)NEXT:NEXT:GOTO120 0ELSEPROCnp:NEXT:NEXT:GOTO1200 1070" ** 1080DEFPROCnp:K%=K%+1:T%(K%)=P%:U%(K%)= C%:C%=C%-1:PRINT'" ";P%;" is not a prime . (";K%;")":PRINT:FORD=1TO3000:NEXT:N%=S %:ENDPROC 1090" ** 1100DEFPROCprint 1110PRINT 1120IFK%>22 VDU14:PRINT" Press SHIFT to scroll.":PRINT 1130FORL%=1TOK% 1140PRINT" ";L%;") ";T%(L%);" (";U%(L%) ;")" 1150NEXT 1160PRINT'" Press SPACE to continue." 1170G=GET:VDU15:PRINT 1180ENDPROC 1190" ** 1200ONERRORCLS:GOTO210 1210VDU7:PRINTLIN$:PRINT" LIMIT OF PROG RAM." 1220PRINT'" Press N for list of Non-pri mes. ESC for menu." 1230G$=GET$:IF G$="N" PROCprint:GOTO122 0ELSEGOTO1230 1240REM"  1250REM" Escape 1260ONERROR GOTO1390 1270PRINT'" TIME=";T%DIV6000;"min ";(T% MOD6000)/100;"sec" 1280PRINT'" Press: R to Repeat." 1290IFF%=1PRINTTAB(7)" C to Continue." 1300PRINTTAB(7)" M for Menu." 1310PRINTTAB(7)" ESC to Escape." 1320G$=GET$ 1330IFG$="R"ONF%GOTO450,510,560,710 1340IFG$<>"C"ORF%<>1THEN1370 1350 ONERRORT%=TIME:GOTO1260 1360 C%=Y%:TIME=Z%:PRINT:GOTO490 1370IFG$="M"CLS:GOTO210 1380GOTO1320 1390PRINT:VDU23,1,1;0;0;0;