Sei sulla pagina 1di 5

Sieve of Eratosthenes

Sieve of Eratosthenes
In mathematics, the sieve of Eratosthenes (Greek: ), one of a number of prime number sieves, is a simple, ancient algorithm for finding all prime numbers up to a specified integer.[1] It is one of the most efficient ways to find all of the smaller primes (below 10 million or so).[2] The algorithm is named after Eratosthenes of Cyrene, an ancient Greek mathematician; although none of his works have survived, the sieve was described and attributed to Eratosthenes in the Introduction to Arithmetic by Nicomachus.[3]

Algorithm description
Sift the Two's and Sift the Three's, The Sieve of Eratosthenes. When the multiples sublime, The numbers that remain are Prime. Anonymous
[4] [5] [6]

Sieve of Eratosthenes: algorithm steps for primes below 121 (including optimization of starting from prime's square).

A prime number is a natural number which has exactly two distinct natural number divisors: 1 and itself. To find all the prime numbers less than or equal to a given integer n by Eratosthenes' method: 1. Create a list of consecutive integers from 2 to n: (2, 3, 4, ..., n). 2. Initially, let p equal 2, the first prime number. 3. Starting from p, count up in increments of p and mark each of these numbers greater than p itself in the list. These numbers will be 2p, 3p, 4p, etc.; note that some of them may have already been marked. 4. Find the first number greater than p in the list that is not marked; let p now equal this number (which is the next prime). 5. If there were no more unmarked numbers in the list, stop. Otherwise, repeat from step 3. When the algorithm terminates, all the numbers in the list that are not marked are prime. As a refinement, it is sufficient to mark the numbers in step 3 starting from p2, as all the smaller multiples of p will have already been marked at that point. This means that the algorithm is allowed to terminate in step 5 when p2 is greater than n. This does not appear in the original algorithm.[7] Another refinement is to initially list odd numbers only (3, 5, ..., n), and count up using an increment of 2p in step 3, thus marking only odd multiples of p greater than p itself. This refinement actually appears in the original description.[8] This can be generalized with wheel factorization, forming the initial list only from numbers coprime with the first few primes and not just from odds, i.e. numbers coprime with 2.[9]

Sieve of Eratosthenes

Incremental sieve
An incremental formulation of the sieve[10] generates primes indefinitely (i.e. without an upper bound) by interleaving the generation of primes with the generation of their multiples (so that primes can be found in gaps between the multiples), where the multiples of each prime p are generated directly, by counting up from the square of the prime in increments of p (or 2p for odd primes).

Trial division
Trial division can be used to produce primes by filtering out the composites found by testing each candidate number for divisibility by its preceding primes. It is often confused with the sieve of Eratosthenes, although the latter directly generates the composites instead of testing for them. Trial division has worse theoretical complexity than that of the sieve of Eratosthenes in generating ranges of primes.[10] When testing each candidate number, the optimal trial division algorithm uses just those prime numbers not exceeding its square root. The widely known 1975 functional code by David Turner[11] is often presented as an example of the sieve of Eratosthenes[9] but is actually a sub-optimal trial division algorithm.[10]

Example
To find all the prime numbers less than or equal to 30, proceed as follows. First generate a list of integers from 2 to 30:
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30

First number in the list is 2; cross out every 2nd number in the list after it (by counting up in increments of 2), i.e. all the multiples of 2:
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30

Next number in the list after 2 is 3; cross out every 3-rd number in the list after it (by counting up in increments of 3), i.e. all the multiples of 3:
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30

Next number not yet crossed out in the list after 3 is 5; cross out every 5-th number in the list after it (by counting up in increments of 5), i.e. all the multiples of 5:
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30

Next number not yet crossed out in the list after 5 is 7; the next step would be to cross out every 7-th number in the list after it, but they are all already crossed out at this point, as these numbers (14, 21, 28) are also multiples of smaller primes because 7*7 is greater than 30. The numbers left not crossed out in the list at this point are all the prime numbers below 30:
2 3 5 7 11 13 17 19 23 29

Sieve of Eratosthenes

Algorithm complexity
Time complexity in random access machine model is that the prime harmonic series asymptotically approaches The bit complexity of the algorithm is .
[12]

operations, a direct consequence of the fact . bit operations with a memory requirement of operations and

The segmented version of the sieve of Eratosthenes, with basic optimizations, uses bits of memory.[13]

Implementation
In pseudocode:[14] Input: an integer n > 1 Let A be an array of Boolean values, indexed by integers 2 to n, initially all set to true. for i = 2, 3, 4, ..., while i n/2: if A[i] is true: for j = 2i, 3i, 4i, ..., while j n: A[j] = false Now all i such that A[i] is true are prime. Large ranges may not fit entirely in memory. In these cases it is necessary to use a segmented sieve where only portions of the range are sieved at a time.[15] For ranges so large that the sieving primes could not be held in memory, space-efficient sieves like that of Sorenson are used instead.[16]

Arithmetic progressions
The sieve may be used to find primes in arithmetic progressions.[17]

Euler's sieve
Euler's proof of the zeta product formula contains version of the sieve of Eratosthenes in which each composite number is eliminated exactly once. It, too, starts with a list of numbers from 2 to n in order. On each step the first element is identified as the next prime and the results of multiplying this prime with each element of the list are marked in the list for subsequent deletion. The initial element and the marked elements are then removed from the working sequence, and the process is repeated:
[2] (3) 5 [3] [4] [5] [...] 7 9 11 11 11 13 15 17 19 21 23 25 27 29 31 33 35 37 39 41 43 45 47 49 51 53 55 57 59 61 63 65 67 69 71 73 75 77 79 13 13 17 19 17 19 17 19 23 25 23 23 29 31 29 31 29 31 35 37 37 37 41 43 41 43 41 43 47 49 47 49 47 53 55 53 53 59 61 59 61 59 61 65 67 67 67 71 73 71 73 71 73 77 79 77 79 79 ... ... ... ...

(5) 7 (7)

(11) 13

Here the example is shown starting from odds, after the 1st step of the algorithm. Thus on k-th step all the multiples of the k-th prime are removed from the list. If generating a bounded sequence of primes, when the next identified prime exceeds the square root of the upper limit, all the remaining numbers in the list are prime. In the example given above that is achieved on identifying 11 as next prime, giving a list of all primes less than or equal to 80.

Sieve of Eratosthenes Note that numbers that will be discarded by some step are still used while marking the multiples, e.g. for the multiples of 3 it is 3 3 = 9, 3 5 = 15, 3 7 = 21, 3 9 = 27, ..., 3 15 = 45, ... .

References
[1] Horsley, Rev. Samuel, F. R. S., " or, The Sieve of Eratosthenes. Being an Account of His Method of Finding All the Prime Numbers," Philosophical Transactions (16831775), Vol. 62. (1772), pp. 327347 (http:/ / www. jstor. org/ stable/ 106053). [2] The Prime Glossary: "The Sieve of Eratosthenes", http:/ / primes. utm. edu/ glossary/ page. php?sort=SieveOfEratosthenes, references 16. November 2008. [3] Nicomachus, Introduction to Arithmetic, I, 13. (http:/ / www. archive. org/ stream/ nicomachigerasen00nicouoft#page/ 29/ mode/ 1up) [4] Clocksin, William F., Christopher S. Mellish, Programming in Prolog, 1981, p. 174. ISBN: 3540110461. [5] Merritt, Doug (December 14, 2008). "Sieve Of Eratosthenes" (http:/ / c2. com/ cgi/ wiki?SieveOfEratosthenes). . Retrieved 2009-03-26. [6] Nyknen, Matti (October 26, 2007). "An Introduction to Functional Programming with the Programming Language Haskell" (http:/ / www. cs. uku. fi/ ~mnykanen/ FOH/ lectures. pdf). . Retrieved 2009-03-26. [7] Nicomachus, ibid., p. 31, where e.g. 93 is marked as a multiple of 31. [8] Nicomachus, ibid., p. 31, where only odd numbers appear in the table. [9] Colin Runciman, "FUNCTIONAL PEARL: Lazy wheel sieves and spirals of primes" (http:/ / portal. acm. org/ citation. cfm?id=969898), Journal of Functional Programming, Volume 7 Issue 2, March 1997; also here (http:/ / citeseerx. ist. psu. edu/ viewdoc/ summary?doi=10. 1. 1. 55. 7096& rank=3). [10] O'Neill, Melissa E., "The Genuine Sieve of Eratosthenes" (http:/ / www. cs. hmc. edu/ ~oneill/ papers/ Sieve-JFP. pdf), Journal of Functional Programming, Published online by Cambridge University Press 9 October 2008 doi:10.1017/S0956796808007004, pp. 10, 11 (contains two incremental sieves in Haskell: a priority-queuebased one by O'Neill and a listbased, by Richard Bird). [11] Turner, David A. SASL language manual. Tech. rept. CS/75/1. Department of Computational Science, University of St. Andrews 1975. [12] Pritchard, Paul, "Linear prime-number sieves: a family tree," Sci. Comput. Programming 9:1 (1987), pp. 1735. [13] A. O. L. Atkin and D. J. Bernstein, "Prime sieves using binary quadratic forms" (http:/ / www. ams. org/ mcom/ 2004-73-246/ S0025-5718-03-01501-1/ S0025-5718-03-01501-1. pdf), Mathematics of Computation 73 (2004), pp. 10231030. [14] Sedgewick, Robert (1992). Algorithms in C++. Addison-Wesley. ISBN0-201-51059-6., p. 16. [15] Crandall & Pomerance, Prime Numbers: A Computational Perspective, second edition, Springer: 2005, pp. 121124. [16] J. Sorenson, The pseudosquares prime sieve (http:/ / citeseerx. ist. psu. edu/ viewdoc/ summary?doi=10. 1. 1. 94. 1737), Proceedings of the 7th International Symposium on Algorithmic Number Theory. (ANTS-VII). [17] J. C. Morehead, "Extension of the Sieve of Eratosthenes to arithmetical progressions and applications", Annals of Mathematics, Second Series 10:2 (1909), pp. 88104 (http:/ / www. jstor. org/ stable/ 1967477).

External links
Interactive JavaScript Page (http://www.hbmeyer.de/eratosiv.htm) Sieve of Eratosthenes (http://demonstrations.wolfram.com/SieveOfEratosthenes/) by George Beck, Wolfram Demonstrations Project. Sieve of Eratosthenes in Haskell (http://www.haskell.org/haskellwiki/Prime_numbers) Sieve of Eratosthenes algorithm illustrated and explained. Java and C++ implementations. (http://www.algolist. net/Algorithms/Number_theoretic_algorithms/Sieve_of_Eratosthenes) A simplified C++ version of a sieve based upon the Sieve of Eratosthenes (some minor optimization) (http:// www.prophetsprayer.com/?page_id=59#SoEg1) A highly optimized Sieve of Eratosthenes in C (http://sites.google.com/site/bbuhrow/) A parallel implementation in C# (http://paratechnical.blogspot.com/2011/01/ c-implementation-of-parallel-sieve-of.html) SieveOfEratosthenesInManyProgrammingLanguages c2 wiki page (http://c2.com/cgi/ wiki?SieveOfEratosthenesInManyProgrammingLanguages)

Article Sources and Contributors

Article Sources and Contributors


Sieve of Eratosthenes Source: http://en.wikipedia.org/w/index.php?oldid=465232565 Contributors: 1onlyone1, A5b, Aftermath1983, Aisano, Alansohn, Albert1ls, Alexander Iwaschkin, Alexb@cut-the-knot.com, Altenmann, Andrei Stroe, Anton Mravcek, Arathel, Arkwatem, Ausples, AzraelUK, BitRAKE, Bkil, BlueMoonlet, Briaboru, Buhrow52, CRGreathouse, Calcyman, Card, Charles Matthews, ChrisHamburg, Ciphers, Cuddlyable3, Cuneas, Curps, Cybercobra, D, DRE, Daniel Mahu, Daniel Simanek, Danio, Deucalionite, Electronish, Epbr123, Epipelagic, Felixaldonso, Ferkel, Fernandopabon, Frank.corr, Furrykef, F, Gamepower, Gandalf61, Genjix, Gesslein, Gf uip, Giftlite, Gnfnrf, GoatGuy, GoldenDragon819, Grafen, GregorB, Gte351s, H2g2bob, Haham hanuka, HamburgerRadio, Hdante, HenrikHellstrom, Heryu, Hockeystud91, Houbysoft, Huan086, IMneme, Ibrocade, IngerAlHaosului, Ixfd64, J.delanoy, JHunterJ, Jitse Niesen, JohnBlackburne, JohnCD, Justin W Smith, Juuitchan, Jzakiya, Kaiba, Kaleetos, Kapil.xerox, Kku, Knutux, Kurzon, L Kensington, LOL, Lee Daniel Crocker, Legion fi, Legomaniac9, Leif, Linas, Lissajous, Little Mountain 5, MC10, MSGJ, Macaldo, Madmardigan53, Marklarr, Martin451, Mastadonz, MatLieskovsky, Matt Crypto, Mblumber, McGeddon, Mentifisto, Michael Hardy, Modeha, Mormegil, MrPinky, Mrh30, NickyMcLean, NihilistDandy, Nimur, Nutiketaiel, Nyinyithann, Obscuranym, OlEnglish, Olathe, Onmywaybackhome, Ozone77, Pandas, Para15000, ParadoxGreen, Peterlin, Philip Trueman, PhotoBox, Pleasantville, PrimeHunter, Quantumgeek, Qwertyus, Rebollo fr, Recognizance, RedHillian, Reywas92, Rich Farmbrough, Rinbar70, Roar888, Ronja Addams-Moring, Rothorpe, Rrburke, Ruud Koot, Sabbut, Sadads, Saperaud, SaveTheRbtz, Sbowers3, Sceptre, Scimitar, Slawekb, Soelvstein, Soler97, Spencer, Spiff, StormWillLaugh, Stwalkerster, Subversive.sound, Syncategoremata, THEN WHO WAS PHONE?, TSP, Teorth, The Thing That Should Not Be, TheLoneRanger, TimothyRias, Tktktk, Tommy2010, Ultraexactzz, V79benno, Vary, W.F.Galway, WagTheWonderDog, WillNess, Wknight94, Wolfram.Tungsten, Wookie2u, X7q, Xp54321, Yamamoto Ichiro, Yanick.rochon, Yosif4444, Zfishwiki, Zuejay, , 330 , anonymous edits

Image Sources, Licenses and Contributors


Image:Sieve of Eratosthenes animation.gif Source: http://en.wikipedia.org/w/index.php?title=File:Sieve_of_Eratosthenes_animation.gif License: GNU Free Documentation License Contributors: 6Sixx, Brian0918, Jahobr, JohnBlackburne, Ricordisamoa, Travrsa, Waldir, WillNess, 3 anonymous edits

License
Creative Commons Attribution-Share Alike 3.0 Unported //creativecommons.org/licenses/by-sa/3.0/

Potrebbero piacerti anche