Sie sind auf Seite 1von 11

TheSieveofZakiya

by
JabariZakiya2008
Introduction
In[1]IintroducedamethodtogeneratealltheprimesuptoNusingaspecificclassofprimegenerator
functions.IshowedhowIdevelopedtheseprimegeneratorsandhowIdevelopedsoftwarecodeto
implementanewprimesievealgorithmusingthem.Benchmarksofthissievealgorithmagainstthe
SieveofEratosthenes(SoE)showedthismethod,theSieveofZakiya(SoZ),wasalwaysfasterforany
primegeneratorused,andwasfasterthantheSieveofAtkin(SoA)forcertaingenerators.Inthispaper
Iexpanduponthoseresultsin[1]andpresentnewfindingsinacomprehensiveandstructuredmanner.
MathNotationandDefinitions
SoZPrimeGenerators(PG)haveformPn=mk+(1,ri),k=0..t.Ifnisaprime(e.g.P3)itdenotesthe
orderpnofthegenerator,i.e.itgeneratesprimes>n.Anevenn(e.g.P30)signifiesaPG'smodulus,
andthesePGcangenerateprimesthefirstriandtheirorderpnisthefirstprimelessthanr1in(1,ri).
Thevaluem(apositiveeveninteger)isthemodulus.AllprimesNgreaterthanaPG'sordermust
satisfy(Nmodm) {1,ri}wheretheriareoddintegerresiduespn<ri<m.Alternativelytobeprime
(N{1,ri})modm=0.Theset{1,ri}hasanevennumberofmembersandthusanoddnumberofri
residues(1=r0),andr1isthefirstprime>pnandthelargest/lastri=m1.
IclassifyPGsasbeingstrictlyprime(SP)ornonSP.SPPGsmodulihavetheformP!(n)=2*3*5...n,
i.e.P!(n)=piisthefactorialoftheprimesupton,andtheirtotalresiduesare:res=#{1,ri}=(pi1).
Inmyoriginalpaper[1](UltimateSievePrimeSieveofZakiya)IidentifiedhowtogeneratethePGs.
Iidentifiedandtested5SPPGs:P3,P5,P7,P11,P13and4nonSPPGs:P60,P110,P150,P180.
Averyinterestingpropertyoftheset{1,ri}isthemembersformmodularcomplement(mc)pairs(mcp)
e.g.m=si+sjwheres {1,ri}and ri=m*res/2,fori=0..(res1),whereres=#{1,ri}istotalresidues.

Themodularcomplementpairscomein3Forms:
1)aprimeriisamodularcomplementtoanotherprimerjor1.
2)anonprimeriisamodularcomplementtoaprimerjor1.
3)anonprimeriisamodularcomplementtoanothernonprimerj.
Group1PGshavemcofonlyForm1,Group2PGshavemcofForm1and2,andGroup3hasall3.
Fromabove,Group1onlyhasP3andP5,Group2includesP7,P60,P90,P110,P120,P150,P180,andGroup3
containsP11andP13.AllPGswithmoduli>210(P7)willhavemcpofGroup3.
TheSieveofZakiya(SoZ)
1)Firstcreateanarray(list)ofprimecandidatevalues.
InitializeanarrayoflengthN/2tofalse(nil),whosevaluesrepresenttheoddnumbersN(orarange).
Thengeneratealistoftheprimecandidates(pc)uptoN(oroverarange)usingPn=mk+(1,ri),k=0..t.
Setthearraytotrue(orthesevalues)fortheindexvaluesequaltothepcvaluesthatareN.Thenset
thearraytotrueattheindexvaluesfortheprimespi<m.
2)Performasieveprocesstoeliminatethenonprimes.
IdentifyallthenonprimesinthearrayusingP'n=pj*[mk+(ri,m+1)]=(pj*m)k+pj*(ri,m+1),k=0..t,
wherethepjsaretheprimespn<pjsqrt(N)andp1=r1foreachPG.
2008/12/22

ThefollowingisanillustrationoftheSoZusingP5=30k+(1,7,11,13,17,19,23,29).
Firstcreatealistofprimecandidateswhere:n1=30k+1,n2=30k+7,...n8=30k+29fork=1..t
Table1.
k
0

10

11

12

13

14

15

16

17

n1

31

61

91 121 151 181 211 241 271 301 331 361 391 421 451 481 511

n2

37

67

97 127 157 187 217 247 277 307 337 367 397 427 457 487 517

n3

11

41

71 101 131 161 191 221 251 281 311 341 371 401 431 461 491 521

n4

13

43

73 103 133 163 193 223 253 283 313 343 373 403 433 463 493 523

n5

17

47

77 107 137 167 197 227 257 287 317 347 377 407 437 467 497 527

n6

19

49

79 109 139 169 199 229 259 289 319 349 379 409 439 469 499 529

n7

23

53

83 113 143 173 203 233 263 293 323 353 383 413 443 473 503 533

n8

29

59

89 119 149 179 209 239 269 299 329 359 389 419 449 479 509 539

NowgeneratethenonprimesbyP'5=(pj*30)k+pj*(7,11,13,17,19,23,29,31),fork=0..t,and
n1k=(pj*30)k+pj*7,n2k=(pj*30)k+pj*11,...n8k=(pj*30)k+pj*31
ShadedvaluesinTable1arethenonprimesshowninTable2.
Table2.
pj n10 n20 n30 n40 n50 n60 n70 n80 n11 n21 n31
91

n41

n51

n61

n71

n81

n12

49

77

119 133 161 203 217 259 287 301 329 343 371 413 427 469

11

77

121 143 187 209 253 319 341 407 451 473 517 539 583 649 671 737

13

91

143 169 221 247 299 377 403 481 533 559 611 637 689 767 793 871

17

119 187 221 289 323 391 493 527 629 697 731 799 833 901 1003 1037 1139

19

133 209 247 323 361 437 551 589 703 779 817 893 931 1007 1121 1159 1273

23

161 253 299 391 437 529 667 713 851 943 989 1081 1127 1219 1357 1403 1541

29

203 319 377 493 551 667 841 899 1073 1189 1247 1363 1421 1537 1711 1769 1943

31

217 341 403 527 589 713 899 961 1147 1271 1333 1457 1519 1643 1829 1891 2077

37

259 407 481 629 703 851 1073 1147 1369 1517 1591 1739 1813 1961 2183 2257 2479

41

287 451 533 697 779 943 1189 1271 1517 1681 1763 1927 2009 2173 2419 2501 2747

Therowsaretheprimespn<pjsqrt(N),thecolumnsaretheresidues[mk+(ri,m+1)],andthetable
entriesaretheirproducts.Thefirstproduct(row1:col1)isalwaysp12=r12.Thesubsequentithrow/col
productsalongthediagonalequalpi2untilthefirstnikcolumnvaluebecomesnonprime.ForP'5this
becomesfirsttruewhenk=1,butforlargerPGswithnonprimerithisbecomestruefork=0.Allthe
productstotheleftofthediagonalvaluesarepreviouslycomputedvalues.Thissymmetrycanbeused
toperformanoptimizedsieveprocessthatbecomesmoreefficientasNgetsbigger.

Table3showstheprogressionofthediagonalproductsfactorsforTable2,whichissimilarforallPGs.
Table3.
nik 7 11 13 17 19 23 29 31 37 41 43 47 49 53 59 61 67 71 73 77 79 83
pi

11 13 17 19 23 29 31 37 41 43 47 53 59 61 67 71 73 79 83 89 97

UsingN=500asanexample,theprimecandidates(pc)500willbegeneratedfork=1..16inTable1.
FromTable2,allthevalues500generatedbytheprimes<sqrt(500)=22,i.e.(7,11,13,17,19)are
eliminatedfromthepclistinTable1(theshadedvalues).Thus,thenonshadedTable1values500
fork=1..16,plustheprimes<m=30,(2,3,5,7,11,13,17,19,23,29),aretheprimes500.
SofromTable1,thenumberofpc500are(16*8)2=126,minus41nonprimes,leaving85primes,
plusthe10primes<30,givesPi(500)=95.
Coding
TheSoZPGsonlyuseoddintegers.Thus,thenaturaloddnumberscanbeconvertedintosequential
arrayindexvaluesbyi=(n3)/2,wherenisanaturaloddvalue,iitsconvertedvalueand/isinteger
division,andthusn=2i+3.Sotheindicesmaptointegersin,as03,15,27,39,etc.
1)Identifytheprimecandidates.
Forlanguageswithstaticarrays,initializeasievearrayto"false"thatis(N+m)elementslong.Thelast
indexvalueforthesievevalueswillbelndx=(N1)/2,sousingtheconvertedvalues,thesievearray
willhavelength(lndx+(m/2)).Thencreatevariablesn1..nkforthePG'sresiduesfortheirconverted
values,wheren1=1(11).Then,whilethelargestni<lndx,incrementeachnibym/2,thensetthe
arrayto"true"(ortheindexvalue)fortheseindexvalues.Whenfinished,setthesievearrayto"true"
fortheindicesequaltotheconvertedprimes<m(i.epii;30,51,72,114,etc.).This
completestheprocesstoidentifyandloadthesievearraywithalltheprimecandidatesN.
2)PerformtheSoZsieve.
Setavariablen=0(usedtocountthenumberofprimessqrt(N)thesievewillgothrough).Withinan
outerloop,startingatthevalueforr1anditeratinguptothesqrt(N),foreachloopvaluei,ifsieve[i]not
true(i.enotprime)getthenextouterloopvalue.Ifsieve[i]true,createvariablesthatequalalltheres
numberproductspjandcomputep1=i*r1....pn=i*(m+1)andk=i*m.Thencomputex=k*n/res,then
incrementn.Addxtoallthep1....pnproducts.(Thiswillskipcomputingthevaluestotheleftofthe
diagonalinTable2,andbegineachres*s,s=0..tprimesgroupatthestartofeachi*[r1,(m+1)]group.
Ifthisstepisomitted,thevaluestotheleftoftheTable2diagonalwillberecomputed.)Then,while
pn<lndx,setthesievearrayateachpjvalueto"false",incrementeachpjbyk,andrepeat.Whenpn
becomes>lndx,startingatp1,testtoseeifpj<lndx,andsetsieve[pj]=falseifso.Performthistest
fortheremainingpjvalues.Thisendsthesievefortheouterloopvaluei.Getnextiandrepeattill
outerloopfinished.Thesievearrayindicesnowrepresenttheconvertedprimesfrom3..Nwheretheir
valuesare"true".Convertingtheseindicestonaturalnumbers,andappendingto2,givesPi(N).
Atthewebsitelistedin[1]codetoperformvariousSoZPGsareavailableinRuby,Python,andForth,
includingbenchmarkstocompareperformancesagainsttheSieveofEratosthenes(SoE)andSieveof
Atkin(SoA).Ageneralizedroutine(sozPn)isincludedintheRubycode,whichcanperformtheSoZ
forallGroup1,2PGsandP11,andwhensuppliedanarrayofForm3mcpvalues,anyGroup3PGs(to
thelimitationsofthesystem'smemory).BelowisRubycodeforanimplementationofsozP3.
3

Code 1.

class Integer
def sozP3
# all prime candidates > 3 are of form 6*k+(1,5)
# initialize sieve array with only these candidate values
# where sieve contains the odd integers representatives
# convert integers to array indices/vals by i = (n-3)/2
n1, n2 = -1, 1; lndx = (self-1)/2; sieve = []
while n2 < lndx
n1 +=3; n2 += 3; sieve[n1] = n1; sieve[n2] = n2
end
#now initialize sieve array with (odd) primes < 6, resize array
sieve[0]=0; sieve[1]=1; sieve = sieve[0..lndx-1]
n = 0
# primes count
1.step((Math.sqrt(self).to_i - 3)/2,1) do |i|
next unless sieve[i]
# j i 5i 7i 6i p1=5i, p2=7i, mp=6i
# 5->1 11 16 15
j = (2*i)+3; p1 = (2*j)+i; p2 = p1+j; mp = p2-i
x = mp*(n/2); n += 1
# x = mp*(n/res) res = 2
p1 += x; p2 += x
while p2 < lndx
sieve[p1] = nil; sieve[p2] = nil; p1 += mp; p2 += mp
end
sieve[p1] = nil if p1 < lndx
end
return [2] if self < 3
[2] + sieve.compact.map {|i| (2*i)+3 }
end
end

PartitioningoveraRange
InsteadoffindingalltheprimesuptoN,theSoZcanbeusedtofindprimesoverarangeassimilarly
donewithSoEpartitioningmethods.Prichard[3]andTomasOliveiraeSilva[4]haveconstructed
methodsbasedontheSoEtoperformsievingoverarange.Thereisnothinginherentintheir
methodologythatwouldprohibittheuseofSoZPGsinsteadoftheSoE,infact,theycouldmakethem
muchfaster.
ParallelProcessing
BoththeprimecandidatesgenerationprocessPn=mk+(1,ri)andthesieveP'n=pj*[mk+(ri,m+1)]can
inherentlybedoneinparallel.Sincetheresiduesactasindependentvectorsintothenumberspacethey
canbeusedsimultaneouslywhileactingonthesamearray.Aminimallyfullyoptimizedparallel
implementationwoulduseasmanyprocessingthreads/coresasaPG'stotalresidues.Moreoptimized
versionscoulddividethenumberspaceuptoNintoranges,forsome/allresidues,forincreasedspeed.
PrimeGenerators
ForSP(strictlyprime)PGsthenumberoftotalresiduesare:res=#{1,ri}=(pi1).
Example:forP13,res=#{1,ri}=(pi1)=(21)(31)(51)(71)(111)(131)=1*2*4*6*10*12=5760.
SPPGsP3andP5haveGroup1mcp,P7hasGroup2mcp,andSPPGsP11haveGroup3mcp.SP
PGsalsohaveincreasingordersandincreasingmodulus/resratios.Sincethereleaseofmypaper[1],I
haveverifiedP17.Mysystem'smemorylimitationseffectivelyprohibitverificationoflargerSPPGs.
P17=510510k+(1,[92159riresiduevalues:42324primes,23339Form2mc,26496Form3mc])
4

Apparently,aninfinitenumberofPGswithmodulithataremultiplesof30exist.Thosefrom30thru
180canbeconstructedbysuccessivelyadding30*k,k=1..t,tothebaseresiduesofP5/30.
P5/30=30k+(1,7,11,13,17,19,23,29)
P60=60k+(1,7...29)+(31,37,41,43,47,49,53,59)
P90=90k+(1,7...59)+(61,67,71,73,77,79,83,89)
P120=120k+(1,7...89)+(91,97,101,103,107,109,113,119)
P150=150k+(1,7...119)+(121,127,131,133,137,139,143,149)
P180=180k+(1,7...149)+(151,157,161,163,167,169,173,179)
Note,allthesePGshaveorderpn=5(r1=7),andthesamemodulus/resratio(15/4),andhavemcpof
Group2.Also,theirmodulusvaluesareallproductsofonlypowersof2,3,and5.
30=21*31*51;60=22*31*51;90=21*32*51;120=23*31*51;150=21*31*52;180=22*32*51
Though210isamultipleof30,it'saSPmodulus(210=2*3*5*7)andthuspn=7(r1=11).Ithasmcp
ofGroup2,anditsmodulus/resratio(210/48=35/8)isalsohigherthanthelowermodulivalues.
ValidPGsformultiplesof30moduli240allhavemcpofGroup3,andwillhavem/resratios15/4,
exceptforspecialmultiples.Form=2*210=420andm=6*210=1260,420/96=1260/288=35/8,the
sameform=210.Thelargestvalidmultipleof30modulusIverifiedwasm/res=30000/8000=15/4.
OfthePGsverifiedtodate,P110istheoddity.Withpn=2(r1=3),ithasthelowestmodulus/resratio
(110/42=55/21)andistheslowest,andhasGroup2mcp.
Efficiency
Theefficiencyofaprimegenerator,ef(Pn),attemptstoquantifyhowwellaPGcanfindprimesovera
spanofmnumbersusingthenumberres=#{1,ri}residues.Astheratiom/resbecomesbiggeraPG
willbeabletofindmoreprimesfaster.ForSPPGsthisbecomesln(pj/(pj1))forpj3.
Table4.
Pn

P3

P5

P60

P110

P150

P180

P7

modulus

30

60

110

150

180

210

2310 30030 510510

residues

16

42

40

48

48

480

ef(Pn)=ln(m/res)

P11

P13

P17

5760

92160

1.099 1.322 1.322 0.963 1.322 1.322 1.476 1.571 1.651

1.712

ComplexityAnalysis
[2]statestheSoAcomputesprimesuptoNusingO(N/loglogN)operations,whiletheSoEisO(N).

AnefficientSoEconsidersonlytheoddnumbersNasprimecandidates,anditeratesovertheodd
primessqrt(N)toeliminatetheiroddvaluedmultiplesstartingatpi2,whichidentify[(Npi2)/2pi+1]
multiplesforeachprime.
ExampleN=100:theprimes10,are3,5,7;thereare16multiplesfor3(9,15,21,27,...99);and8
multiplesfor5(25,35,45,...95);and4multiplesfor7(49,63,77,91).Thus16+8+4=28,minus2
duplicates,identify26oddnonprimes100.Thuspi(100)=5026odd+1even(2)=25primes.

TheSoZidentifiesatmostres*(N/m)primecandidatesforeachPG.Ittheniteratesthroughtheprimes
piwherepn<pisqrt(N)andp1=r1foreachPG.Eachpispansatmost(Npi2)/mpigroupsofresidues
frompi2toN,whicharethepimultiplestotherightofthediagonalinTable2,whichmakestheapprox
maxnumberofmultiplesforeachpioverthisspan:res*(Npi2)/mpi.Thenumberofpimultiplesupto
pi2inTable2issimply:res*pi/m.Here(pi/m)isthepercentageofresiduegroupsspannedbypi,and
thenres*pi/misthenumberofresidueterms.Thus,thetotalnumberofmultiples<Nforeachpiis:
(1)

res*(Npi2)/mpi+res*pi/m=res*N/mpi

Thisrepresentsthenumberofmultiplesforthenonoptimizedsieve,startingatcolumn1inTable2.
ExampleN=1000:m=30,res=8,thusforpi=19thereare8*1000/(30*19)=14multiples<1000,and
fromTable2forpi=19itshowsthereare,infact,13multipleof19<1000.
Therefore,themaximumnumberofallprimemultiplesforpn<pisqrt(N)is:
(2)

res*N/mpi=res*N/m1/pi

ThenbyMertensTheorem:1/piloglognwherethepiarealltheprimesupton=N1/2.Thiswill
beabiggerupperboundthannecessaryinthiscasesince(2)onlysumstheprimespn<piN1/2.So
thetotalnumberofprimemultiples(nonprimes)eliminatedbythesievewillbe:
(3) res*N/m1/pi(res*N/m)(loglogN1/2)E(N)
whereE(N)isanerrorterminvolvingtheprimesumsforpipn.
Amoreoptimizedsievecanbeperformedbydeterminingwhichkthresiduegroupeachdiagonalpi
startsinandskippingovertheprecedingkresiduegroupcomputationsforthat.Soifpistartsinthe
diagonalgroupfork=3,subtractallthek*res=3*resduplicatesforthe3residuegroupsfork=0,1,2
fromtheleftofthediagonalvaluesinTable2.Fortheresiduegrouppistartsin,itwillcomputeall
thoseresmultiples.Fora(resxres)matrix,thiswillresortinres*(res1)/2duplicatemultiplesbeing
produced(lefthalfmatrixvalues)justonceforalltheresnumberpithatspanthatresiduegroup.
FromTable2,whereres=8,thefirst8sieveprimeswillproduce8*(81)/2=28duplicates,whichare
thevaluestotheleftofthediagonalforthefirst8x8(pi*ri)values.Thenstartingwithp9=37,thenext8
sieveprimeswillskipthefirst8x8groupk=0multiples,andproducethemultiplesfork=1,etc.
Thoughitmayseemalgorithmicallysuboptimaltogeneratetheseduplicateproductsitisactually
computationallyoptimalforcoding.Allthenikproductsforpiforeachkthresiduegrouphavetobe
producedanyway,astheyformthebaseresiduegroupsuccessivevaluesofmpiareaddedtogenerate
thenikforthehigherkresiduegroups.InCode1thisisthepurposeofx=mp*n/res,whichcalculates
thediagonalkthresiduegroupeachpistartsin.However,aftertheni0productsandthekthbasegroup
x=kmpiarecomputed,alltheotherproductsarecomputedasjusttheadditionsofthevaluempitothe
precedingkthresiduegroupvalues.Nomultiplicationsarenecessary.
kmaxequalstheintegerdivisionofthesieveprimespn<piN1/2byres,i.e.kmax=[Pi(N1/2)Pi(pn)1]/res.
Eachkthgroupofresnumberprimeswillskipoverk*(res*res)=k*res2duplicates.Thus,fork=0,no
res2duplicateswillbeskipped;fork=1,1res2groupwillbeskipped;then2,3,etc.Thus,thetotalres2
groupsskippedarek=1+2+...+kmax=kmax*(kmax+1)/2andthereforethemaximumnumberoftotal
duplicatesskippedisres2*(kmax*(kmax+1)/2).
6

Thus,anoptimizedreducednumberofsievemultiplesshowninTable2canbeapproximatedas(4):
(4)

res*N/m1/pires2*(kmax*(kmax+1)/2)

andthusfrom(3)wegetfor(4)
(5)

(res*N/m)(loglogN1/2)res2*(kmax*(kmax+1)/2)

TogetanexpressioninN,andtomakethematheasier,IwillusekmaxPi(N1/2)/res,wherehereI'll
usePi(N1/2)N1/2/(logN1/2).Afterfactoringthisproduces:
(6)

res2*(kmax*(kmax+1)/2)=(N+res*(logN1/2)*N1/2)/(2*(logN1/2)2)

Replacingthisis(5)givesthenumberofnonprimestobeeliminatedbytheoptimizedsievetobe:
(7)

(res*N/m)(loglogN1/2)(N+res*(logN1/2)*N1/2)/(2*(logN1/2)2)

Again,thefirsttermin(7)representsallthenonprimesshowninTable2foraPG.AsthePGsorder
grow,fewernonprimesarecomputedbythefirstterm,asres/mgetssmaller,whilethesecondterm
getsbiggerasresincreases,whichincreasinglyreducesthefirsttermvaluemore.
Therealnumbersofprimemultiples(nonprimes)canbeempiricallydeterminedbycountingthetimes
Code1isinsidetheinnersieveloop(thewhile...endloop)andmultiplyingthatbyres.Thevariablen
inCode1countstheactualnumberofsieveprimespn<piN1/2fromwhichkmaxcanbedetermined.
PlottingthesevaluesagainstvaluesofNforthePGsproducesarevealingfamilyofcurveswhich
graphicallydepictthecomparativeperformanceofthePGs.
Thus,thetotaloperationalcomplexityoftheSoZincludestheprimecandidatesgenerationprocess,
whichcreatesres*(N/m)pc,andthesieveprocess,whicheliminates(7)numberofnonprimes.This
alsorepresentsthetimecomplexity,asthegeneration/eliminationofaprimecandidateismerelyan
arraywriteandvariableincrementwithaconstant.

ThesievearrayonlyneedsN/21bitelements,andtheindexvaluescontainthenumbersinformation.
Forcodingpurposes[N/2+res](byte)elementsareused.Memoryalsoisusedtostoreandupdatethe
resresiduesvaluesandressieveproductvalues.Onlyonesetofresvariablesisneededtoholdthese
valuesastheyareusedinseparateoperations.Thus,thelargerPGswilluseslightlymorememoryfor
largerresvalues,inadditiontothesievearray.
ThetotalSoZoperationalcomplexityis,thus,minimal.Itusesmostlyarray/memwritesandvariable
additions,andahandfulofmultiplicationsanddivisions(shifts)andarithmetic/booleancomparisons.
Asqrtoperationisoptionalifthesqrt(n)ispassedinasaparameter.Thisissimilartotheoperational
complexityoftheSoE.TheSoZisalsosimpleandeasytoimplementasaparallelalgorithmduetoits
algorithmicandoperationalsimplicity.Incomparison,theSoA,andquadraticresiduesieves,aremore
algorithmicallyandoperationallycomplex(aswellasinherentlyhardertounderstandandcode).They
alsoneedtoperformmanymodulooperations(divisions)onincreasinglybiggernumbersrequiring
comparativelymorecomputationaltime.
Therefore,theSoZcanbeimplementedtogeneratetheprimesuptoanumberNfasterthananyother
methodknowntopresentlyexist.
7

Primalitytesting
TheSoZcanbeperformedbackwardstocreateadeterministicprimalitytester.TheeasiestPGtouse
forthisisP3=6k+(1,5).Thebasicprocessinvolvesthefollowingsteps:
1)returntrueifNis2,3,or5.
2)returnfalseifNis1oranevennumber.
3)returnfalseifforN>5(Nmod6)isnotequalto1or5ORNisamultipleof5.
4)Ifnpassesthesetests,usetheprimecandidatesproducedbyP3,startingatk=1,ascandidatesforthe
primessqrt(N),andperformthereverseofthesieveprocessP'n=pj*[mk+(ri,m+1)],whichinthis
caseispj*[6k+(5,7)].IfNisamultipleofpj>mitwillbecomputedbythesieve.Thus,foreverypc
n,subtracttheproductsp=5nandq=7nfromN,andifanyoftheseresultsisdivisibleby6n(m*pj)
thenNisnonprime.IfNcanpassthistestforallthepcsqrt(N)itisaprime.
ExampleforN=119:
N=119passesthefirstthreetests(119mod6=5),andthesqrt(119)=10.Soweonlyneedtotestitfor
n=7,whichgivesp=7*5=35,q=7*7=49,andk=7*6=42,andthendo:(11949)=70and
(11935)=84;with(70mod42)=38,but(84mod42)=0(84isdivisibleby42),thus119isnonprime.
Inotherwords,startingwith5*7,addingsuccessively(42+42)=7*6*2(pmk),creates35+84=119.
ExampleforN=113:
Thesqrt(113)=10,soagain,n=7,andp=35,q=49,andk=42.Now(11349)=64and(11335)=78,
andbecauseneither(64mod42)or(78mod42)iszero,then113isprime.
Code2(inRuby)usesP3togeneratetheprimecandidates,andisfasterthantheprimalitytesterin[1].
Code 2.
class Integer
def primz?
n = self.abs
return true if [2, 3, 5].include? n
return false if n == 1 || n & 1 == 0
return false if n > 5 && (! [1, 5].include?(n%6) || n%5 == 0)
sqrtN = Math.sqrt(n).to_i
n1, n2 = 7, 11
while n1 <= sqrtN
# pi= 5*i, ki = 6*i,
qi = 7*i
p1 = 5*n1; k1 = p1+n1; q1 = k1+n1
return false if (n-p1)%k1 == 0 || (n-q1)%k1 == 0
p2 = 7*n2; k2 = p2+n2; q2 = k2+n2
return false if (n-p2)%k2 == 0 || (n-q2)%k2 == 0
n1 += 6; n2 += 6
end
return true
end
end

HybridconfigurationscanbecreatedbyusingbiggerPGstogeneratetheprimesforthereversesieve.
SincetheSPPGsarethemostefficient,theywillgeneraterelativelyfewernonprimes,thusmaking
verifyingprimes(whichgoesthrualltheresiduessqrt(N))potentiallyfaster.Codingwise,this
meanstherewillberesnumberoftuples(p,q,k)tests,withtheresiduesiteratedbym.Theultimate
hybrid,however,computestheprimessqrt(N),andfeedsthissolelyprimeslisttothereversesieve.
Fortestingreallylargenumbersthisisthefastestmethod.Code3showsthishybridmethod.
8

Code 3.
class Integer
def primz?(input=nil) # accepts array of primes: 7...sqrt(n)
n = self.abs
return true if [2, 3, 5].include? n
return false if n == 1 || n & 1 == 0
return false if n > 5 && (! [1, 5].include?(n%6) || n%5 == 0)
# if primes array is provided, use it, otherwise compute it
primes = input || (Math.sqrt(n).to_i.sozP11 - [2,3,5])
primes.each do |i|
# p= 5*i,
k = 6*i, q = 7*i
p = 5*i;
k = p+i; q = k+i
return false if (n-p)%k == 0 || (n-q)%k == 0
end
return true
end
end

InCode3,theprimes7pisqrt(N)canbeinputasasortedarrayorcomputedinternallyifnoarray
isinput.HeresozP11generatestheprimes,butotherappropriateoravailablegeneratorscanbeused.
Thismethodisperfectlysuitedforbeingimplementedbyclusterfarms,whichcandividetheprimes
intosmallliststobeprocessedinparallelbyasmanycores/threadsthereareavailabletoapplytoit.
ThoughtsandConclusion
BecausethePGsresiduesincludetheprimes<mtheyreallydon'tstarttoshineuntilNismuchlarger
thanm2.ThischaracteristiccanmasktheutilityofthelargerPGs.SoforP13withm=30030,N
shouldbemuchgreaterthan9billion,andforP17,Nshouldtobemuchgreaterthan260billiontoget
theoptimumbenefitfromusingit.Thisposesaproblemforassessingthetrueperformanceofthese
generatorson32bitPCs,whichmayonlyhaveafewGBsofmemory,butneedslotsofmemoryto
representandprocessthesemegasizednumbers.
Asatisfyingspinofffromtheprimegeneratorsworkistheidentificationofasimple,short,andelegant
deterministicprimalitytestalgorithm.Thisshouldhavewideuseformanyapplications.
Alsofromempiricalanalysis,Ifound2goodapproximationsofpifortheprimesuptoN:
(8)

(1/2)*N2/(logN)<(5/2)*N2/(logN)pi

OneareaofresearchIseeaneedforischaracterizingwheretheForm3mcpcomefromanalytically.I
knowhowtocomputationallygeneratethem,butnotanalytically.Infact,ageneralareaofresearch
shouldbetoassessifthePGscanbemodeledintoafamilyofcurvesthatwouldcreatethem,muchthe
samewayItooktheresiduesfromthecurvesidentifiedin[2]andcombinedthemintotheP60PG.
Thenthere'sthecontinuingworkinfinding/verifyingallthepossiblePGsofformPn=mk+(1,ri).This
includestheSPandnonSPgenerators,anddeterminingifallthemultiplesof30moduli,exceptforthe
specialones(multiplesofSPmoduli,etal?)haveam/resratioof15/4.There'salotofinterestingmath
goingonhere,whichIhopespurslotsofacademic,etal,interestandinsights.
Lastly,withtheincreasingproliferationofmulticoresystems,clusterfarms,andmegaGBsofusable
memory,theSoZPGscanbeusedtogreatlyincreasethesearch,andtesting,oflargeprimes.
9

References
[1]UltimatePrimeSieveSieveofZakiya(SoZ),JabariZakiya,June2008,
http://www.4shared.com/dir/7467736/97bd7b71/sharing.html
[2]PrimeSievesUsingBinaryQuadraticForms,A.O.L.Atkin&DanielJ.Bernstein
http://cr.yp.to/ntheory.html#primesieves
[3]FastCompactPrimeNumberSieves(amongothers),PaulPritchard
http://historical.ncstrl.org/tr/temp/ocr/cstr.cs.cornell.edu/TR81473
[4]SegmentedSieveofEratosthenes,TomasOliveiraeSilva
http://www.ieeta.pt/~tos/software/prime_sieve.html
Thispaperalsoavailableat:http://www.4shared.com/dir/7467736/97bd7b71/sharing.html
Contact:jzakiya(at)yahoo(dot)com

10

SieveofZakiya(SoZ)PrimeGenerators
P3=6k+(1,5)
P5=30k+(1,7,11,13,17,19,23,29)
P60=60k+(1,7,11,13,17,19,23,29,31,37,41,43,47,49,53,59)
P110=110k+(1,3,7,9,11,13,17,19,21,23,27,29,31,37,39,41,43,47,49,51,53,57,59,61,63,
67,69,71,73,79,81,83,87,89,91,93,97,99,101,103,107,109)
P150=150k+(1,7,11,13,17,19,23,29,31,37,41,43,47,49,53,59,61,67,71,73,77,79,83,89,
91,97,101,103,107,109,113,119,121,127,131,133,137,139,143,149)
P180=180k+(1,7,11,13,17,19,23,29,31,37,41,43,47,49,53,59,61,67,71,73,77,79,83,89,91,
97,101,103,107,109,113,119,121,127,131,133,137,139,143,149,151,157,161,163,167,169,
173,179)
P7=210k+(1,11,13,17,19,23,29,31,37,41,43,47,53,59,61,67,71,73,79,83,89,97,101,103,
107,109,113,121,127,131,137,139,143,149,151,157,163,167,169,173,179,181,187,191,
193,197,199,209)
P11=2310k+(1,13,17,19,23,29,31,37,41,43,47,53,59,61,67,71,73,79,83,89,97,101,103,
107,109,113,127,131,137,139,149,151,157,163,167,169,173,179,181,191,193,197,199,
211,221,223,227,229,233,239,241,247,251,257,263,269,271,277,281,283,289,293,299,
307,311,313,317,323,331,337,347,349,353,359,361,367,373,377,379,383,389,391,397,
401,403,409,419,421,431,433,437,439,443,449,457,461,463,467,479,481,487,491,493,
499,503,509,521,523,527,529,533,541,547,551,557,559,563,569,571,577,587,589,593,
599,601,607,611,613,617,619,629,631,641,643,647,653,659,661,667,673,677,683,689,
691,697,701,703,709,713,719,727,731,733,739,743,751,757,761,767,769,773,779,787,
793,797,799,809,811,817,821,823,827,829,839,841,851,853,857,859,863,871,877,881,
883,887,893,899,901,907,911,919,923,929,937,941,943,947,949,953,961,967,971,977,
983,989,991,997,1003,1007,1009,1013,1019,1021,1027,1031,1033,1037,1039,1049,1051,
1061,1063,1069,1073,1079,1081,1087,1091,1093,1097,1103,1109,1117,1121,1123,1129,
1139,1147,1151,1153,1157,1159,1163,1171,1181,1187,1189,1193,1201,1207,1213,1217,
1219,1223,1229,1231,1237,1241,1247,1249,1259,1261,1271,1273,1277,1279,1283,1289,
1291,1297,1301,1303,1307,1313,1319,1321,1327,1333,1339,1343,1349,1357,1361,1363,
1367,1369,1373,1381,1387,1391,1399,1403,1409,1411,1417,1423,1427,1429,1433,1439,
1447,1451,1453,1457,1459,1469,1471,1481,1483,1487,1489,1493,1499,1501,1511,1513,
1517,1523,1531,1537,1541,1543,1549,1553,1559,1567,1571,1577,1579,1583,1591,1597,
1601,1607,1609,1613,1619,1621,1627,1633,1637,1643,1649,1651,1657,1663,1667,1669,
1679,1681,1691,1693,1697,1699,1703,1709,1711,1717,1721,1723,1733,1739,1741,1747,
1751,1753,1759,1763,1769,1777,1781,1783,1787,1789,1801,1807,1811,1817,1819,1823,
1829,1831,1843,1847,1849,1853,1861,1867,1871,1873,1877,1879,1889,1891,1901,1907,
1909,1913,1919,1921,1927,1931,1933,1937,1943,1949,1951,1957,1961,1963,1973,1979,
1987,1993,1997,1999,2003,2011,2017,2021,2027,2029,2033,2039,2041,2047,2053,2059,
2063,2069,2071,2077,2081,2083,2087,2089,2099,2111,2113,2117,2119,2129,2131,2137,
2141,2143,2147,2153,2159,2161,2171,2173,2179,2183,2197,2201,2203,2207,2209,2213,
2221,2227,2231,2237,2239,2243,2249,2251,2257,2263,2267,2269,2273,2279,2281,2287,
2291,2293,2297,2309)
P13=30030k+(1,[5759riresiduevalues:3242primes,1431Form2mc,1086Form3mc])
P17=510510k+(1,[92159riresiduevalues:42324primes,23339Form2mc,26496Form3mc])
11

Das könnte Ihnen auch gefallen