Efficient Prime Number Generating Algorithms

1

Efficient Prime Number Generating Algorithms
PGsimple1
In any typical programming language course, the student gets a project to write a program that generates prime numbers. This is considered to be a relatively easy task which is assigned within the first few weeks of the course. As I'm sure you are aware, a few simple and effective algorithms can be used to complete the assignment within as little as a few minutes. In the following examples, I will be using the Python2.5 programming language to demonstrate such algorithms and compare their efficiencies. The first algorithm we shall consider will begin with the integer 2 and proceed to select each successive integer as a potential prime, (pp), checking for primacy by testing to see if it can be factored by any previously identified primes, then storing each newly verified prime in a prime set (ps) array. pp=2 ps=[pp] lim=raw_input("\nGenerate prime numbers up to what number? : ") while pp<int(lim): pp+=1 test=True for a in ps: if pp%a==0: test=False if test: ps.append(pp) return ps Note: The code given above does not constitute a complete program. See Appendix A for a complete program including a user interface. Such a rudimentary algorithm takes a strictly brute force approach to effectively achieve the goal of identifying each prime number and storing it in an array. I am sure you would agree that this is also about the least efficient means of generating prime numbers. Runtime Data primes up to 100 1000 10000 100000 1000000 pgsimple1 takes .00100 .02800 1.6980 130.44 10732 sec sec sec sec sec

These are the best time results taken from 5 test runs at each limit. This table records the runtimes for pgsimple1 up to each limit indicated. Please accept that these runtimes and all of the runtimes given throughout this document may differ somewhat from those which you may get running the same program on a computer with different hardware or software as mine, which has an AMD Turion 64 1.9GHz with 2GB RAM, 160GB HDD and Windows Vista.

Efficient Prime Number Generating Algorithms

2

PGsimple2
A first step at improving this algorithm might be efficiently selecting potential primes. It is most common to see such a device in algorithms which start with the integer 3 and proceed by selecting successive potential primes through the odd integers only. This reduces by half the total number of potential primes which must be tested. pp=2 ps=[pp] pp+=1 ps.append(pp) lim=raw_input("\nGenerate prime numbers up to what number? : ") while pp<int(lim): pp+=2 test=True for a in ps: if pp%a==0: test=False if test: ps.append(pp) return ps Now, brute force has been augmented by some simple logic to significantly improve efficiency, reducing the number of potential primes by half. Runtime Data primes up to 100 1000 10000 100000 1000000 10000000 pgsimple2 takes 0.0 .01400 .85700 65.240 5392.9 458123 times faster compared to pgsimple1 ~ 2.00 1.98 2.00 1.99 ~

These are the best time results taken from 5 test runs at each limit. This table records the runtimes for pgsimple2 and how many times faster it completes the run up to each limit compared to pgsimple1. Note that the efficiency remains close to double that of pgsimple1 at any limit. Even at this speed, it is still quite impractical to generate 8 digit primes or more. But, I did it just to see how long it would take.

PGsimple3
The next most obvious improvement would probably be limiting the testing process to only checking if the potential prime can be factored by those primes less than or equal to the square root of the potential prime, since primes larger than the square root of the potential prime will be complimentary factors of at least one prime less than the square root of the potential prime. pp=2 ps=[pp] pp+=1 ps.append(pp) lim=raw_input("\nGenerate prime numbers up to what number? : ") while pp<int(lim):

1 200 649 ~ These are the best time results taken from 5 test runs at each limit. Note that the longer the program is run.0 . but we shall go on. at this point. This table records the runtimes for pgsimple3 and how many times faster it completes the run up to each limit compared to pgsimple1 and pgsimple2. the more significant the efficiency becomes.979 705. as none of them can be factored by 2.8 58. most programmers have exhausted their ability or desire to continue improving the efficiency of the prime number generator. This algorithm makes truly significant strides in efficiency and.67 13.Efficient Prime Number Generating Algorithms pp+=2 test=True sqrtpp=sqrt(pp) for a in ps: if a>sqrtpp: break if pp%a==0: test=False if test: ps. we can remove the first prime number from the set of primes which we test potential primes against. it is no longer necessary to test potential primes against all the primes less than the square root of the prime. then recombining them at the end to send the complete set back to the function call.1220 26.00300 . Therefore.06200 1.append(pp) return ps Runtime Data primes pgsimple3 faster compared up to takes pgsimple2 100 1000 10000 100000 1000000 10000000 100000000 0.4 116 398 ~ ~ ~ 4. This requires dividing the prime set (ps) array into excepted prime (ep) and test prime (tp) arrays. pp=2 ep=[pp] pp+=1 tp=[pp] ss=[2] lim=raw_input("\nGenerate prime numbers up to what number? : ") while pp<int(lim): pp+=ss[0] test=True sqrtpp=sqrt(pp) for a in tp: . PGsimple4 Recognizing that by using a skip number of 2 to select only odd potential primes.37 18780 times faster compared to pgsimple1 times to 3 ~ 9.33 27.

At the same time.00 4 These are the best time results taken from 5 test runs at each limit. Together. increases in efficiency multiply as more primes are eliminated from the testing process in the more advanced version of the program which I will show you next. I apologize for any confusion or inconvenience this may have caused the reader. These errors can easily be corrected at the convenience of the student programmer. While the efficiency of selecting potential primes allows the program to sift through a greater range of numbers per second the longer the program is run.54 18766 times faster compared to pgsimple3 ~ 1. Worry not. but were not necessary to illustrate the performance of the algorithms. Runtime Data primes up to 100 1000 10000 100000 1000000 10000000 100000000 pgsimple4 takes 0. with some explanation to help the reader fully comprehend how the algorithm works.01 1.insert(0.0 .00 1. but without the comments. Please disregard syntactical errors which occur in the user interface such as “the 1th prime”.00300 .Efficient Prime Number Generating Algorithms if a>sqrtpp: break if pp%a==0: test=False if test: tp.734 702.8 This algorithm efficiently selects potential primes by eliminating multiples of previously identified primes from consideration and minimizes the number of tests which must be performed to verify the primacy of each potential prime. PG7.05200 1. it can be generalized and made somewhat elegant. Further skip sets can be developed to eliminate the selection of potential primes which can be factored by each prime that has already been identified. . line by line. minimizing the number of tests which must be performed on each potential prime. This table records the runtimes for pgsimple4 and how many times faster it completes the run up to each limit compared to pgsimple3. (but rises at a slower rate compared to other algorithms). This example is fully commented.19 1. the number of tests which need to be performed on each potential prime does continue to rise. A complete program including a user interface.reverse() [tp.1140 26. Although this process is more complex. instead of “1st”. and the inclusion of the last prime generated in the completed array even though it may be larger than the user defined limit.append(pp) ep.a) for a in ep] return tp In the next version it will be shown why we put the skip number (2) into a skip set (ss) array. we can continue to eliminate from the set of test primes each of the primes which the skip sets eliminate multiples of.01 1. these processes bring greater efficiency to generating prime numbers. can be found in Appendix B. What improvement in efficiency? Note that there is only a marginal increase in efficiency compared to pgsimple3.00 1. making the generation of even 10 digit verified primes possible within a reasonable amount of time on a PC.

not needed for the verification test. it may be useful to understand that future skip sets will contain more than one member. and is the quantity of members of the previous skip set multiplied by one less than the value of the prime which the new skip set will exclude multiples of. setting the first member to be the range of the first skip set. which is 3. the quantity of which can be calculated. The skip set which eliminates multiples of primes up through 5 will have (5-1)*2=8 members. """ ss=[pp] """ Initialize the array for the skip set. (xp). being (pp=2). """ sqrtlim=sqrt(float(lim)) """ Get the square root of the upper limit.the skip set which eliminates multiples of primes up through 3 will have (3-1)*1=2 members.Efficient Prime Number Generating Algorithms lim=raw_input("\nGenerate prime numbers up to what number? : ") """ Get an upper limit from the user to determine the generator's termination point. """ pp=2 """ Initialize the variable for the potential prime. and are the sum of the members of the skip set. The use of an extended primes array is technically unnecessary. setting it at a single member. Another 5 . """ rss=[ss[0]] """ Initialize an array for the ranges of each skip set. (2). This will be the upper limit of the test prime array for primes used to verify the primacy of any potential primes up to (lim). setting it to begin with the first prime number. """ ep=[pp] """ Initialize the array for primes which the skip set eliminate multiples of. since the previous skip set had 2 members. Future skip sets will have ranges which can be calculated. Although the value of the quantity of members in the skip set is never needed in the program. since the previous skip set had 1 member. Example . Primes greater than (sqrtlim) will be placed in an array for extended primes. which is (ss[0]=2). etc. setting the first member as (pp=2) since the first skip set will eliminate multiples of 2 as potential primes. """ pp+=1 """ Advance to the first potential prime. but helps to clarify that we have minimized the size of the test prime array.

""" xp=[] """ Initialize an array for extended primes which are larger than the square root of the user defined limit (lim) and not needed to verify potential primes against. """ rss. the use of an extended primes array is technically unnecessary. since there are no TEST primes less than the square root of 5. multiplied by the value of the largest prime which the new skip set will exclude multiples of. """ pp+=ss[0] """ Advance to the next potential prime.append(npp) """ Add a member to the array of test primes. """ i=0 """ Initialize a variable for keeping track of which skip set range is current. Also note that 3 is a verified prime. so 2*3=6. """ tp. which is the previous potential prime. which has only one member at this time and whose value is (ss[0]=2). since we do not yet have a skip set that excludes multiples of 3. setting its value as (pp=5). (pp=3).Efficient Prime Number Generating Algorithms method of calculating the range will also be shown below. """ while npp<int(lim): """ Loop until the user defined upper limit is reached. setting the first member as (pp=3).append(rss[i]*tp[0]) """ Add a member to the array of skip set ranges. the member being the most recently identified prime. leaving it empty for now. since there are no primes less than the square root of 3. """ i+=1 6 . (rss[0]=2). the value being the value of the previous skip set range. Note that 5 is a verified prime without testing. Again. This value is needed to define when to begin constructing the next skip set. but helps to clarify that we have minimized the size of the test prime array. """ tp=[pp] """ Initialize an array for primes which are needed to verify potential primes against. """ npp=pp """ Initialize a variable for the next potential prime. plus the value of the next member of the skip set. (tp[0]=3). so 3+2=5. without testing. (npp=5).

then assign the potential prime variable the value of the next potential prime.Efficient Prime Number Generating Algorithms """ Increment the skip set range identifier. """ for q in tp: """ Loop through the array of the primes necessary for verification of the next potential prime. """ if npp<=rss[i]+1: pp=npp """ If the next potential prime is still within the range of the next skip set. """ npp=pp+n """ Assign the next potential prime the value of the potential prime plus the value of the current member of the skip set. which will be the limit for the verification process. """ elif npp%q==0: """ If the test prime IS a factor of the next potential prime. """ sqrtnpp=sqrt(npp) """ Get the square root of the next potential prime. then end testing through the 'for q' loop. """ while npp<rss[i]+1: """ Loop until the next skip set range is surpassed. """ test=True """ Set the verification flag to True. """ if sqrtnpp<q: break """ If the test prime is greater than the square root of the next potential prime. since data through that range is needed before constructing the next skip set. then end the 'for n' loop. the potential prime variable is not changed and the current value remains the starting point for constructing the next skip set. assigning the variable (n) the value of the next member of the skip set. """ if npp>int(lim): break """ If the next potential prime is greater than the user defined limit. """ test=False """ Then set the verification flag to 7 . """ for n in ss: """ Loop through the current skip set array. Otherwise.

so we may begin constructing a new skip set which will exclude multiples of primes up through the value of the first member of the test prime set. leaving it empty for now. """ break """ And end testing through the 'for q' loop. continue testing through the 'for q' loop. """ while pp<(rss[i]+1)*2-1: 8 . """ if npp>int(lim): break """ If the next potential prime is greater than the user defined limit.append(npp) """ And if the next potential prime is less than or equal to the square root of the user defined limit. """ lrpp=pp """ Initialize a variable for the last relevant potential prime and set its value to the value of the potential prime. """ """ Otherwise. """ nss=[] """ Initialize an array for the next skip set. """ """ At this point. the range of the next skip set has been reached. """ if test: """ If the next potential prime has been verified as a prime number.Efficient Prime Number Generating Algorithms False since the next potential prime is not a prime number. continue the 'while npp<rss[i]+1' loop. """ if npp<=sqrtlim: tp. """ else: xp. from being selected as potential primes. then add it to the array of primes which potential primes must be tested against. (tp[0]). add it to the array of primes not needed to verify potential primes against. """ """ Otherwise. """ """ Then continue through the 'for n' loop.append(npp) """ Otherwise. """ if npp>int(lim): break """ If the next potential prime is greater than the user defined limit. then end the 'while npp<rss[i]+1' loop. then end the 'while npp<int(lim)' loop.

""" """ Otherwise. """ sqrtnpp=sqrt(npp) """ Get the square root of the next potential prime. """ test=True """ Set the verification flag to True. which will be the limit for the verification process. """ if test: """ If the next potential prime has been verified as a prime number. """ test=False """ Then set the verification flag to False since the next potential prime is not a prime number. then end the 'for n' loop. """ if sqrtnpp<q: break """ If the test prime is greater than the square root of the next potential prime.Efficient Prime Number Generating Algorithms """ Loop until the construction of the new skip set has gone through the range of the new skip set. continue testing through the 'for q' loop. then end testing through the 'for q' loop.append(npp) """ And if the next potential prime is less than or equal to the 9 . """ for q in tp: """ Loop through the array of the primes necessary for verification of the next potential prime. """ elif npp%q==0: """ If the test prime IS a factor of the next potential prime. """ if npp>int(lim): break """ If the next potential prime is greater than the user defined limit. """ npp=pp+n """ Assign the next potential prime the value of the potential prime plus the value of the current member of the skip set. """ if npp<=sqrtlim: tp. """ break """ And end testing through the 'for q' loop. """ for n in ss: """ Loop through the current skip set array.

or not.Efficient Prime Number Generating Algorithms square root of the user defined limit. """ """ Otherwise. then it is relevant to the construction of the new skip set and a member must be included in the new skip set for a potential prime to be selected. """ pp=npp """ Assign the variable for the potential prime the value of the next potential prime. since the 10 . """ if npp>int(lim): break """ If the next potential prime is greater than the user defined limit.append(npp) """ Otherwise. then end the 'while npp<(rss[i]+1)*2-1' loop. then end the 'while npp<int(lim)' loop. continue the 'while npp<(rss[i]+1)*2-1' loop. """ """ Then continue through the 'for n' loop. the value of which is the difference between the last relevant potential prime and the next potential prime. then add it to the array of primes which potential primes must be tested against. Note that this is the case regardless of whether the next potential prime was verified as a prime.append(npp-lrpp) """ Add a member to the next skip set. """ lrpp=npp """ Assign the variable for the last relevant potential prime the value of the next potential prime. """ nss. """ else: xp.append(tp[0]) """ Add a new member to the excluded primes array. add it to the array of primes not needed to verify potential primes against. """ if npp>int(lim): break """ If the next potential prime is greater than the user defined limit. """ ep. """ ss=nss """ Assign the skip set array the value of the new skip set array. """ if npp%tp[0]!=0: """ If the next potential prime was NOT factorable by the first member of the test array.

reverse() """ Flip the array of test primes.0 .76 5. """ """ Then continue through the 'while npp<int(lim)' loop. """ return xp """ Send the completed array of all primes up to the user defined limit back to the function call.8 faster compared up to takes pgsimple4 100 1000 10000 100000 1000000 10000000 100000000 1000000000 0. .92 4.6220 120.83 6.append(rss[i]*tp[0]) """ Add a member to the skip set range array with the value of the range of the next skip set. """ ep.53 2752.01800 .89 3.insert(0.00 2. """ """ At this point the user defined upper limit has been reached and the generator has completed finding all of the prime numbers up to the user defined limit.insert(0. """ [xp. """ tp.0 94.3 459 1909 ~ ~ ~ ~ 3.Efficient Prime Number Generating Algorithms newly constructed skip set will exclude all multiples of primes through the first member of the test prime array.reverse() """ Flip the array of excluded primes.82 ~ These are the best time results taken from 5 test runs at each limit.00100 . """ Runtime Data primes pg7. """ [tp.a) for a in ep] """ Add each member of the flipped array into the beginning of the test primes array. """ npp=lrpp """ Assign the next potential prime the value of the last relevant potential prime.a) for a in tp] """ Add each member of the flipped array into the beginning of the extended primes array. """ rss.28400 5.1 65786 times faster compared to pgsimple1 times to 11 ~ 28. """ del tp[0] """ Delete the first member from the test prime array since future potential primes will not have to be tested against this prime.

" x="\nPlease choose a number that is less than" y="a prime number.Efficient Prime Number Generating Algorithms This table records the runtimes for pg7. 12 A note from the author Thank you for taking the time to study the algorithm and I hope that it has inspired you. please email a copy of your work to cfo@mfbs.len(ps). " n="From: " o="To: " p="or" q="is out of the range of the set generated. Note that again the longer the program is run.org." tt=lim=0 return ps def ui(a): m="\nDo you wish to review the numbers? Enter y for yes or q to quit." r="and" s="There are none between" t="\nPlease pick between 0" u="\nThey are the" v="th thru" w="th members." ."\nwith". the least efficient algorithm lim=raw_input("\nGenerate prime numbers up to what number? : ") pp=2 ps=[pp] bt=time() while pp<int(lim): pp+=1 test=True for a in ps: if pp%a==0: test=False if test: ps.8 and how many times faster it completes the run up to each limit compared to pgsimple1 and pgsimple4.lim. Appendix A #! /usr/bin/env python from math import sqrt from time import time def pg(): # pgsimple1."members.append(pp) et=time() tt=et-bt a=test=et=bt=pp=0 print "\nIt took".tt. the more significant the efficiency becomes."seconds to generate the prime set up to: ". If you choose to translate this algorithm into another programming language.

g.g else: print [a[h] for h in range(int(f).r."\n".r.f.len(a) f=raw_input(n) g=raw_input(o) if int(g)<int(f): h=f f=g g=h if int(f)<0 or int(g)>len(a): print f.q elif f==g: print s." print "d) Input a number to check its membership in the prime number set.r.f." f=raw_input(C) if f=='a': print t.str(g)+w if f=='b': print t.p.p." print "f) Get the number of members in the prime set between a range of numbers.g. 13 .a[len(a)-1]+1 f=raw_input(n) g=raw_input(o) if int(g)<int(f): h=f f=g g=h if int(f)<0 or int(g)>a[len(a)-1]+1: print f.str(int(f)+1)+v.u." print "b) Pick a range of numbers to view prime numbers in that range. " f=raw_input(m) while f!='q': if f=='y': print "\nChoose a category:" print "a) Pick a range of indexes of members of the prime number set.r.Efficient Prime Number Generating Algorithms z="\nThere are" A="members of the prime set" C="Make a selection or enter q to quit." print "v) View 100 primes at a time.g else: i=0 while a[i]<int(f): i+=1 j=i while i<len(a) and a[i]<=int(g): print a[i].int(g))].q elif f==g: print s." print "e) Get the number of members in the prime set up to a particular number.

q if f=='e': print x.f else: print f.a[len(a)-1]+2 f=raw_input(o) if -1<int(f)<a[len(a)-1]+2: g=0 while a[g]<=int(f): g+=1 if g==len(a): break print z.f.a[len(a)-1]+1 f=raw_input(n) g=raw_input(o) if int(g)<int(f): h=f f=g g=h i=0 if int(f)<0 or int(g)>a[len(a)-1]+1: print f.i): if g==len(a): i=len(a) break print a[g]."is not".g.r.str(i)+w if f=='d': print x.g. "thru".y if int(f)<0 or int(g)>=int(f): break if int(f)>g+1 or int(f)<0: print f.a[len(a)-1]+1 f=raw_input("What number do you want to check? ") for g in a: if int(g)==int(f): print f.A.i."up to".str(int(j)+1)+v.y if int(g)>int(f): print f.q if f=='f': print t.r.A.Efficient Prime Number Generating Algorithms i+=1 print u.g if f=='v': g=0 h=1 while f!='q' and g<len(a): i=h*100 for g in range(100*(h-1). 14 .g else: for j in a: if int(f)<=int(j)<=int(g): i+= 1 elif int(j)>int(g): break print z.f.p."from"."is".q elif f==g: print s.

Efficient Prime Number Generating Algorithms print u."\n"*5 raw_input() 15 Appendix B #! /usr/bin/env python from math import sqrt from time import time def pg(): lim=raw_input("\nGenerate prime numbers up to what number? : ") sqrtlim=sqrt(float(lim)) pp=2 ep=[pp] ss=[pp] pp+=1 i=0 rss=[ss[0]] tp=[pp] xp=[] pp+=ss[0] npp=pp tp.append(rss[i]*tp[0]) bt=time() while npp<int(lim): i+=1 while npp<rss[i]+1: for n in ss: npp=pp+n if npp>int(lim): break if npp<=rss[i]+1: pp=npp sqrtnpp=sqrt(npp) test=True for q in tp: ."Don't go away mad. ") f=raw_input(m) def run(a='r'): while a is 'r': a=raw_input("\nEnter r to run prime generator.append(npp) rss.str(100*(h-1)+1)+v.Just go away.". ") if a!='r': return b=pg() ui(b) if __name__ == "__main__": run() print "\n"*5...str(i)+w h+=1 if g==len(a): break f=raw_input("\nView the next 100 members or enter q to quit.

reverse() [tp.tt.append(npp) if npp>int(lim): break if npp>int(lim): break lrpp=pp nss=[] while pp<(rss[i]+1)*2-1: for n in ss: npp=pp+n if npp>int(lim): break sqrtnpp=sqrt(npp) test=True for q in tp: if sqrtnpp<q: break elif npp%q==0: test=False break if test: if npp<=sqrtlim: tp.append(npp-lrpp) lrpp=npp pp=npp if npp>int(lim): break if npp>int(lim): break ss=nss ep." et=bt=ep=tp=a=tt=lim=0 return xp 16 .len(xp).append(tp[0]) del tp[0] rss.reverse() [xp."\nwith".a) for a in ep] tp."members.insert(0.insert(0.a) for a in tp] print "\nIt took".lim.Efficient Prime Number Generating Algorithms if sqrtnpp<q: break elif npp%q==0: test=False break if test: if npp<=sqrtlim: tp."seconds to generate the prime set up to: ".append(npp) if npp%tp[0]!=0: nss.append(rss[i]*tp[0]) npp=lrpp et=time() i=nss=npp=n=sqrtnpp=test=q=r=lrpp=rss=ss=pp=sqrtlim=0 tt=et-bt ep.append(npp) else: xp.append(npp) else: xp.

q elif f==g: print s.p." print "d) Input a number to check its membership in the prime number set." z="\nThere are" A="members of the prime set" C="Make a selection or enter q to quit.f.str(g)+w if f=='b': 17 . " f=raw_input(m) while f!='q': if f=='y': print "\nChoose a category:" print "a) Pick a range of indexes of members of the prime number set.int(g))]." print "f) Get the number of members in the prime set between a range of numbers.str(int(f)+1)+v.r.len(a) f=raw_input(n) g=raw_input(o) if int(g)<int(f): h=f f=g g=h if int(f)<0 or int(g)>len(a): print f." print "b) Pick a range of numbers to view prime numbers in that range.r. " n="From: " o="To: " p="or" q="is out of the range of the set generated.u.Efficient Prime Number Generating Algorithms def ui(a): m="\nDo you wish to review the numbers? Enter y for yes or q to quit." r="and" s="There are none between" t="\nPlease pick between 0" u="\nThey are the" v="th thru" w="th members.g else: print [a[h] for h in range(int(f).g." print "e) Get the number of members in the prime set up to a particular number." print "v) View 100 primes at a time." f=raw_input(C) if f=='a': print t."\n"." x="\nPlease choose a number that is less than" y="a prime number.

y if int(g)>int(f): print f.a[len(a)-1]+1 f=raw_input(n) g=raw_input(o) if int(g)<int(f): h=f f=g g=h if int(f)<0 or int(g)>a[len(a)-1]+1: print f.p.g else: i=0 while a[i]<int(f): i+=1 j=i while i<len(a) and a[i]<=int(g): print a[i].str(int(j)+1)+v.g."is not".f."is".y if int(f)<0 or int(g)>=int(f): break if int(f)>g+1 or int(f)<0: print f.g.f else: print f.q elif f==g: print s.r.q elif f==g: print s.Efficient Prime Number Generating Algorithms print t.A.r.r.str(i)+w if f=='d': print x.q if f=='f': print t.g.f. i+=1 print u."up to".a[len(a)-1]+1 f=raw_input(n) g=raw_input(o) if int(g)<int(f): h=f f=g g=h i=0 if int(f)<0 or int(g)>a[len(a)-1]+1: print f.r.p.g 18 .a[len(a)-1]+2 f=raw_input(o) if -1<int(f)<a[len(a)-1]+2: g=0 while a[g]<=int(f): g+=1 if g==len(a): break print z.a[len(a)-1]+1 f=raw_input("What number do you want to check? ") for g in a: if int(g)==int(f): print f.q if f=='e': print x.

."Don't go away mad. ") if a!='r': return b=pg() ui(b) if __name__ == "__main__": run() print "\n"*5.g if f=='v': g=0 h=1 while f!='q' and g<len(a): i=h*100 for g in range(100*(h-1).Efficient Prime Number Generating Algorithms else: for j in a: if int(f)<=int(j)<=int(g): i+= 1 elif int(j)>int(g): break print z.i): if g==len(a): i=len(a) break print a[g].i.str(100*(h-1)+1)+v.A.".Just go away.str(i)+w h+=1 if g==len(a): break f=raw_input("\nView the next 100 members or enter q to quit. "thru".f. print u."from". ") f=raw_input(m) def run(a='r'): while a is 'r': a=raw_input("\nEnter r to run prime generator."\n"*5 raw_input() 19 ..

org/ licenses/ by-sa/ 3. 0/ .org/w/index.org/w/index.Article Sources and Contributors 20 Article Sources and Contributors Efficient Prime Number Generating Algorithms  Source: http://en.svg  Source: http://en.svg  License: Public Domain  Contributors: User:Siebrand License Creative Commons Attribution-Share Alike 3. Swift.0 Unported http:/ / creativecommons.php?oldid=1858229  Contributors: Adrignola.wikibooks. Thenub314. 13 anonymous edits Image Sources.php?title=File:50%.wikibooks. Licenses and Contributors File:50%.