Follow along with the video below to see how to install our site as a web app on your home screen.
Note: This feature may not be available in some browsers.
top:
PRINT "number?"
INPUT number
max = SQR(number)
'loop only to the square root
primeslength = -1
prime = 1
FOR i = 2 TO max
isprime = 1
FOR j = 0 TO primeslength
IF i MOD primes(j) = 0 THEN isprime = 0: EXIT FOR
'test against every prime before it
NEXT j
IF isprime = 1 THEN
primeslength = primeslength + 1
primes(primeslength) = i
'store the prime number
IF number MOD i = 0 THEN prime = 0: EXIT FOR
'test the number against the prime number
END IF
NEXT i
IF prime = 0 THEN
PRINT number; "is not prime"
ELSE
PRINT number; "is prime"
END IF
GOTO top
Primality tests: test for speed
(seconds for finding 1000 primes starting from 5000000)
4 methods. Please wait...
Trial Division Time: 7.089844
Wheel factorisation - small wheel Time: 3.191406
Wheel factorisation - bigger wheel Time: 3.398438
sieve of Eratosthenes Time: 1.699219
'Primality tests: test speed
'4 methods (well, two weels go for 1.5 so 3.5 methods)
'FUNCTION primeTrialDivision, requires nothing
'Trial division method, based on code by shanley06
'This is the simplest approach.
'Wheel factorisation. Thanks to link by Agual.
'FUNCTION primeSmallWheel, requires nothing
'Simple program
'FUNCTION primeBiggerWheel, uses some global arrays (initialised below)
'to store wheel data. Program becomes more complicated...
'probably this eats up supposed speed gain.
'FUNCTION primeEratosthenes, uses global array to store already found primes
'based on code by mgh730
DIM i AS INTEGER
'initialisation and code ------------
'wheel data for bigger wheel: 2,3,5
'3 base primes
DATA 3
DATA 2,3,5
'8 spokes
DATA 8
DATA 1, 7, 11, 13, 17, 19, 23, 29
'Wheel variables and initialisation ----------
COMMON SHARED NBase AS INTEGER, NSpokes AS INTEGER, modulo AS INTEGER
'--- COMMON statement for Eratosthenes-based algorithm - BASIC have me put it here
COMMON SHARED primeslength AS INTEGER
READ NBase
DIM SHARED basePrimes(NBase) AS LONG
FOR i = 1 TO NBase: READ basePrimes(i): NEXT i
READ NSpokes
DIM SHARED spokes(NSpokes) AS LONG
FOR i = 1 TO NSpokes: READ spokes(i): NEXT i
modulo = 1
FOR i = 1 TO NBase: modulo = modulo * basePrimes(i): NEXT i
'--- end of wheel initialisation segment
'--- array for primes for Eratosthenes-based algorithm
DIM SHARED primes(2000) AS LONG 'It shows that it used 332 of them
primeslength = 0
primes(0) = 2
'test code starts here ----------------------------
CLS
PRINT "Primality tests: test for speed"
PRINT "(seconds for finding 1000 primes starting from 5000000)"
PRINT "4 methods. Please wait..."
PRINT
DIM number AS LONG
'if you want to see primes list, set wantToSeePrimes to 1
wantToSeePrimes = 0
method$ = "Trial Division"
PRINT method$; TAB(40);
i = 0
t = TIMER
FOR number = 5000000 TO 6000000
IF primeTrialDivision(number) THEN
IF wantToSeePrimes THEN PRINT number
i = i + 1
END IF
IF i = 1000 THEN EXIT FOR
NEXT number
PRINT "Time: "; TIMER - t
method$ = "Wheel factorisation - small wheel"
PRINT method$; TAB(40);
i = 0
t = TIMER
FOR number = 5000000 TO 6000000
IF primeSmallWheel(number) THEN
IF wantToSeePrimes THEN PRINT number
i = i + 1
END IF
IF i = 1000 THEN EXIT FOR
NEXT number
PRINT "Time: "; TIMER - t
method$ = "Wheel factorisation - bigger wheel"
PRINT method$; TAB(40);
i = 0
t = TIMER
FOR number = 5000000 TO 6000000
IF primeBiggerWheel(number) THEN
IF wantToSeePrimes THEN PRINT number
i = i + 1
END IF
IF i = 1000 THEN EXIT FOR
NEXT number
PRINT "Time: "; TIMER - t
method$ = "sieve of Eratosthenes"
PRINT method$; TAB(40);
i = 0
t = TIMER
FOR number = 5000000 TO 6000000
IF primeEratosthenes(number) THEN
IF wantToSeePrimes THEN PRINT number
i = i + 1
END IF
IF i = 1000 THEN EXIT FOR
NEXT number
PRINT "Time: "; TIMER - t
FUNCTION primeBiggerWheel (n AS LONG)
'wheel factorisation (as I got it)
'[URL unfurl="true"]http://primes.utm.edu/glossary/page.php?sort=WheelFactorization[/URL]
'bigger weel
'primes 2, 3, 5
DIM x AS LONG, xx AS LONG, sq AS LONG, i AS INTEGER
sq = SQR(n)
primeBiggerWheel = 1
'first check base primes
FOR i = 1 TO NBase
IF (n MOD basePrimes(i)) = 0 THEN primeBiggerWheel = 0: EXIT FUNCTION
NEXT i
'then go by spoke
'first loop - special (don't want miss 1 or check for it every time)
FOR i = 2 TO NSpokes 'so skip spokes(1), that is 1
IF (n MOD spokes(i)) = 0 THEN primeBiggerWheel = 0: EXIT FUNCTION
NEXT i
'main loop
x = modulo
DO
FOR i = 1 TO NSpokes
xx = x + spokes(i)
IF xx > sq THEN EXIT FUNCTION
IF (n MOD xx) = 0 THEN primeBiggerWheel = 0: EXIT FUNCTION
NEXT i
x = x + modulo
LOOP
END FUNCTION
FUNCTION primeEratosthenes (number AS LONG)
'sieve of Eratosthenes
'based by code by mgh730
DIM max AS LONG, i AS LONG, j AS INTEGER
max = SQR(number)
'loop only to the square root
primeEratosthenes = 1
'first, check by stored numbers
FOR j = 0 TO primeslength
IF number MOD primes(j) = 0 THEN primeEratosthenes = 0: EXIT FUNCTION
NEXT j
'next check the rest
FOR i = primes(primeslength) + 1 TO max
isprime = 1
FOR j = 0 TO primeslength
IF i MOD primes(j) = 0 THEN isprime = 0: EXIT FOR
'test against every prime before it
NEXT j
IF isprime = 1 THEN
primeslength = primeslength + 1
primes(primeslength) = i
'store the prime number
IF number MOD i = 0 THEN primeEratosthenes = 0: EXIT FUNCTION
'test the number against the prime number
END IF
NEXT i
END FUNCTION
FUNCTION primeSmallWheel (n AS LONG)
'wheel factorisation (as I got it)
'[URL unfurl="true"]http://primes.utm.edu/glossary/page.php?sort=WheelFactorization[/URL]
'small weel
'primes 2 and 3. This would give us two spokes: 1 and 5
DIM x AS LONG, sq AS LONG
sq = SQR(n)
primeSmallWheel = 1
'first check base primes
IF (n MOD 2) = 0 THEN primeSmallWheel = 0: EXIT FUNCTION
IF (n MOD 3) = 0 THEN primeSmallWheel = 0: EXIT FUNCTION
'then go by spoke (1st and 5th in a table width 6)
x = 5
DO
IF (n MOD x) = 0 THEN primeSmallWheel = 0: EXIT FUNCTION
x = x + 2
IF (n MOD x) = 0 THEN primeSmallWheel = 0: EXIT FUNCTION
x = x + 4
LOOP UNTIL x > sq
END FUNCTION
FUNCTION primeTrialDivision (n AS LONG)
'based on code by shanley06
DIM x AS LONG
primeTrialDivision = 1
FOR x = 2 TO SQR(n)
IF (n MOD x) = 0 THEN primeTrialDivision = 0: EXIT FUNCTION
NEXT x
END FUNCTION
WIDTH , 50: CLS
CONST maxlng = &H7FFFFFFF
PRINT "PRIME CALCULATOR BY ANTONI GUAL agual@eic.ictnet.es"
DO
PRINT "Maximum prime(5 to "; maxlng; "): "; : INPUT mp&
LOOP UNTIL mp& >= 5 AND mp& <= maxlng
CONST stor = 4750
DIM pr(stor + 1) AS LONG
DIM pr1(stor + 1) AS LONG
pr(1) = 2 * 3
pr1(1) = 3 * 3
ind& = 1
ind2% = 1
pr1(2) = maxlng
sqre& = 3 * 3
test& = 5
OPEN "nul" FOR OUTPUT AS #1
t! = TIMER
PRINT 2, 3,
CNT% = 2
DO
DO
a& = pr1(1)
IF a& >= test& THEN EXIT DO
b& = pr(1)
a& = a& + b&
i% = 1
DO
j% = i% + i%
IF j% > ind2% THEN EXIT DO
IF pr1(j%) > pr1(j% + 1) THEN j% = j% + 1
IF a& <= pr1(j%) THEN EXIT DO
pr(i%) = pr(j%)
pr1(i%) = pr1(j%)
i% = j%
LOOP
pr1(i%) = a&
pr(i%) = b&
LOOP
IF a& > test& THEN
ind& = ind& + 1
PRINT test&,
'the print #1 "." is just for W2000, you can erase it if you want
CNT% = CNT% + 1: IF CNT% = 250 THEN CLS : CNT% = 0: PRINT #1, ".";
IF ind& <= stor THEN pr(ind&) = test&: IF ind2% = stor THEN EXIT DO
END IF
test& = test& + 2
IF test& > sqre& THEN
GOSUB addnew
IF LEN(INKEY$) THEN EXIT DO
END IF
LOOP UNTIL test& > mp&
PRINT : PRINT : PRINT "time for "; ind& + 1; "primes: "; TIMER - t!; "seconds"
a$ = INPUT$(1)
END
addnew:
ind2% = ind2% + 1
a& = pr(ind2%)
sqre& = a& * a&
j% = ind2%
DO
i% = j% \ 2
IF i% = 0 THEN EXIT DO
IF pr1(i%) <= sqre& THEN EXIT DO
pr(j%) = pr1(i%)
pr1(j%) = pr1(i%)
j% = i%
LOOP
pr1(j%) = sqre&
pr(j%) = a& + a&
pr1(j% + 1) = maxlng
RETURN
viewstack:
CLS : FOR ii% = 0 TO ind2% + 1: PRINT pr(ii%), pr1(ii%): NEXT
PRINT test&
a$ = INPUT$(1)
RETURN