You are on page 1of 3

7/1/2015

ThePrimeGlossary:SieveofEratosthenes

SieveofEratosthenes
(anotherPrimePages'Glossary
entries)
Glossary:
Index
Search
Home
Previous
Next
Random
MailEditor
PrimePages:
Home
Search
Index
FAQ
Top5000:
Primes
Provers
Curios

Themostefficientwaytofindallofthesmall primes (sayallthoselessthan


10,000,000)isbyusingasievesuchastheSieveof Eratosthenes (ca240BC):
Makealistofalltheintegerslessthanorequalton(andgreaterthan
one).Strikeoutthemultiplesofallprimeslessthanorequaltothe
square root ofn,thenthenumbersthatareleftaretheprimes.
Forexample,tofindalltheprimeslessthanorequalto30,firstlistthenumbers
from2to30.
23456789101112131415161718192021222324252627
282930
Thefirstnumber2isprime,sokeepit(wewillcoloritgreen)andcrossoutits
multiples(wewillcolorthemred),sotherednumbersarenotprime.
23456789101112131415161718192021222324252627
282930
Thefirstnumberleft(stillblack)is3,soitisthefirstoddprime.Keepitandcross
outallofitsmultiples.Weknowthatallmultipleslessthan9(i.e.6)willalready
havebeencrossedout,sowecanstartcrossingoutat32=9.
23456789101112131415161718192021222324252627
282930
Nowthefirstnumberleft(stillblack)is5,thesecondoddprime.Sokeepitalsoand
crossoutallofitsmultiples(allmultipleslessthan52=25havealreadybeencrossed
out,andinfact25istheonlymultiplenotyetcrossedout).
23456789101112131415161718192021222324252627
282930
Thenextnumberleft,7,islargerthanthesquarerootof30,sothereareno
multiplesof7tocrossoffthathaven'talreadybeencrossedoff(14and28by2,and
21by3),andthereforethesieveiscomplete.Thereforeallofthenumbersleftare
primes:{2,3,5,7,11,13,17,19,23,29}.Noticewejustfoundtheseprimes
withoutdividing.
Thisalgorithmcanbewritteninpseudocodeasfollows
Eratosthenes(n){
a[1]:=0
fori:=2tondoa[i]:=1
p:=2

https://primes.utm.edu/glossary/page.php?sort=SieveOfEratosthenes

1/3

7/1/2015

ThePrimeGlossary:SieveofEratosthenes

whilep2<ndo{
j:=p2
while(j<n)do{
a[j]:=0
j:=j+p
}
repeatp:=p+1untila[p]=1
}
return(a)
}

Thismethodissofastthatthereisnoreasontostorealargelistofprimesona
computeranefficientimplementationcanfindthemfasterthanacomputercan
readfromadisk.Infacttheproblemwiththealgorithmaspresentedaboveisnot
reallyspeed(ituses O (n( log n)loglogn)bitoperations),butratherspace(itis
O(n)).Soforlargenweusuallyuseasegmentedsieve.However,boththetimeand
spacetheoreticallycanbeimprovedforexample,Pritchard's"linearsegmented
wheelsieve"usesO(nlogn)bitoperationsandO(sqrt(n)/loglogn)space.
SeeAlso: TrialDivision
Relatedpages(outsideofthiswork)
ProgramsandpseudocodeforTheSieveofEratosthenes
References:
BH77
C.BayesandR.Hudson,"ThesegmentedsieveofEratosthenes
andprimesinarithmeticprogression,"NordiskTidskr.
Informationsbehandling(BIT),17:2(1977)121127.MR
56:5405
Bressoud89
D.M.Bressoud,Factorizationsandprimalitytesting,Springer
Verlag,NewYork,NY,1989.ISBN0387970401.MR
91e:11150[pseudocodeimplementationonpage19]
[QA161.F3B73]
Pritchard87
P.Pritchard,"Linearprimenumbersieves:afamilytree,"Sci.
Comput.Programming,9:1(1987)1735.MR88j:11087
[Comparisonofvarioussieves][Acomparisonofrecentsieves
suchasthesieveofEratosthenes.]
Riesel94
H.Riesel,Primenumbersandcomputermethodsfor
factorization,ProgressinMathematicsVol,126,Birkhuser
Boston,Boston,MA,1994.ISBN0817637435.MR
95h:11142[aPASCALimplementationonpage6][Anexcellent
referenceforthosewhowanttostarttoprogramsomeofthese
algorithms.CodeisprovidedinPascal.Previouseditionwasvol.
57,1985.]

https://primes.utm.edu/glossary/page.php?sort=SieveOfEratosthenes

2/3

7/1/2015

ThePrimeGlossary:SieveofEratosthenes

ChrisK.Caldwell19992015(allrightsreserved)

https://primes.utm.edu/glossary/page.php?sort=SieveOfEratosthenes

3/3

You might also like