The Sieve of Eratosthenes
Algorithm creator(s)
Eratosthenes of Cyrene
PB author(s)
Eric Pearson
Description
Search for prime numbers using the Sieve of Eratosthenes algorithm.
Note
The basic idea is to identify non-prime numbers; everything left over is prime.
Source
https://forum.powerbasic.com/forum/user-to-user-discussions/source-code/23385-primes-numbers-sieve-of-eratosthenes
See also
n/a
Source Code
Download source code file eratosthenes.bas (Right-click -> "Save as ...")
#COMPILE EXE
#REGISTER NONE
#DIM ALL
FUNCTION PBMain AS LONG
%MaxNum = 800 'INCREASE THIS NUMBER TO FIND MORE AND MORE PRIMES.
DIM Candidate(%MaxNum) AS LOCAL LONG
DIM lStepper AS LOCAL LONG
DIM lScan AS LOCAL LONG
FOR lStepper = 2 TO SQR(%MaxNum)
IF Candidate(lStepper) = 0 THEN
FOR lScan = lStepper*2 TO %MaxNum STEP lStepper
Candidate(lScan) = 1
NEXT
END IF
NEXT
FOR lScan = 1 TO %MaxNum
IF Candidate(lScan) = 0 THEN
PRINT lScan,
END IF
NEXT
WAITKEY$
END FUNCTION