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. It is one of the most efficient ways to find all of the smaller primes (below 10 million or so). 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.
To find all the prime numbers less than or equal to a given integer n by Eratosthenes' method:
- Create a list of consecutive integers from 2 to n: (2, 3, 4, ..., n).
- Initially, let p equal 2, the first prime number.
- 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.
- 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).
- 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.
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. 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.
An incremental formulation of the sieve 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 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.
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 is often presented as an example of the sieve of Eratosthenes but is actually a sub-optimal trial division algorithm.
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:
45 67 89 1011 1213 1415 1617 1819 2021 2223 2425 2627 2829 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:
45 67 8 9 1011 1213 14 15 1617 1819 20 21 2223 2425 26 27 2829 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:
45 67 8 9 1011 1213 14 15 1617 1819 20 21 2223 24 25 26 27 2829 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
Time complexity in random access machine model is O(nlog log n) operations, a direct consequence of the fact that the prime harmonic series asymptotically approaches log log n.
The bit complexity of the algorithm is O(n(log n)(log log n)) bit operations with a memory requirement of O(n).
The segmented version of the sieve of Eratosthenes, with basic optimizations, uses O(n) operations and O(n1 / 2log log n / log n) bits of memory.
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. For ranges so large that the sieving primes could not be held in memory, space-efficient sieves like that of Sorenson are used instead.
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:
 (3) 5 7 9 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 ...  (5) 7 11 13 17 19 23 25 29 31 35 37 41 43 47 49 53 55 59 61 65 67 71 73 77 79 ...  (7) 11 13 17 19 23 29 31 37 41 43 47 49 53 59 61 67 71 73 77 79 ...  (11) 13 17 19 23 29 31 37 41 43 47 53 59 61 67 71 73 79 ... [...]
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.
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, ... .
- ^ 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 (1683–1775), Vol. 62. (1772), pp. 327–347.
- ^ The Prime Glossary: "The Sieve of Eratosthenes", http://primes.utm.edu/glossary/page.php?sort=SieveOfEratosthenes, references 16. November 2008.
- ^ Nicomachus, Introduction to Arithmetic, I, 13. 
- ^ Clocksin, William F., Christopher S. Mellish, Programming in Prolog, 1981, p. 174. ISBN: 3540110461.
- ^ Merritt, Doug (December 14, 2008). "Sieve Of Eratosthenes". http://c2.com/cgi/wiki?SieveOfEratosthenes. Retrieved 2009-03-26.
- ^ Nykänen, 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.
- ^ Nicomachus, ibid., p. 31, where e.g. 93 is marked as a multiple of 31.
- ^ Nicomachus, ibid., p. 31, where only odd numbers appear in the table.
- ^ a b Colin Runciman, "FUNCTIONAL PEARL: Lazy wheel sieves and spirals of primes", Journal of Functional Programming, Volume 7 Issue 2, March 1997; also here.
- ^ a b c O'Neill, Melissa E., "The Genuine Sieve of Eratosthenes", 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-queue–based one by O'Neill and a list–based, by Richard Bird).
- ^ Turner, David A. SASL language manual. Tech. rept. CS/75/1. Department of Computational Science, University of St. Andrews 1975.
- ^ Pritchard, Paul, "Linear prime-number sieves: a family tree," Sci. Comput. Programming 9:1 (1987), pp. 17–35.
- ^ A. O. L. Atkin and D. J. Bernstein, "Prime sieves using binary quadratic forms", Mathematics of Computation 73 (2004), pp. 1023–1030.
- ^ Sedgewick, Robert (1992). Algorithms in C++. Addison-Wesley. ISBN 0-201-51059-6. , p. 16.
- ^ Crandall & Pomerance, Prime Numbers: A Computational Perspective, second edition, Springer: 2005, pp. 121–124.
- ^ J. Sorenson, The pseudosquares prime sieve, Proceedings of the 7th International Symposium on Algorithmic Number Theory. (ANTS-VII).
- ^ J. C. Morehead, "Extension of the Sieve of Eratosthenes to arithmetical progressions and applications", Annals of Mathematics, Second Series 10:2 (1909), pp. 88–104.
- Sieve of Eratosthenes by George Beck, Wolfram Demonstrations Project.
- Sieve of Eratosthenes in Haskell
- Sieve of Eratosthenes algorithm illustrated and explained. Java and C++ implementations.
- A simplified C++ version of a sieve based upon the Sieve of Eratosthenes (some minor optimization)
- A highly optimized Sieve of Eratosthenes in C
- A parallel implementation in C#
- SieveOfEratosthenesInManyProgrammingLanguages c2 wiki page
Primality tests Sieving algorithms Integer factorization algorithms Multiplication algorithms Discrete logarithm algorithms GCD algorithms Modular square root algorithmsCipolla · Pocklington's · Tonelli–Shanks Other algorithmsItalics indicate that algorithm is for numbers of special forms; bold indicates deterministic algorithm for primality tests (current article is always in bold).
Wikimedia Foundation. 2010.
Look at other dictionaries:
sieve of eratosthenes — ˌerəˈtästhəˌnēz Usage: usually capitalized E Etymology: after Eratosthenes fl 3d century B.C. Greek astronomer and geographer : a device for finding prime numbers consisting of the writing down of the odd numbers from 3 up in succession and of… … Useful english dictionary
sieve of Eratosthenes — Date: 1803 a procedure for finding prime numbers that involves writing down the odd numbers from 2 up in succession and crossing out every third number after 3, every fifth after 5 including those already crossed out, every seventh after 7, and… … New Collegiate Dictionary
sieve of Eratosthenes — Math. a method of obtaining prime numbers by sifting out the composite numbers from the set of natural numbers so that only prime numbers remain. [1795 1805] * * * … Universalium
sieve of Eratosthenes — noun An ancient algorithm for finding prime numbers that works by discarding multiples from a list of potential primes … Wiktionary
Eratosthenes — of Cyrene (Greek polytonic|Ἐρατοσθένης; 276 BC 194 BC) was a Greek mathematician, poet, athlete, geographer and astronomer. He made several discoveries and inventions including a system of latitude and longitude. He was the first person to… … Wikipedia
Sieve theory — is a set of general techniques in number theory, designed to count, or more realistically to estimate the size of, sifted sets of integers. The primordial example of a sifted set is the set of prime numbers up to some prescribed limit X .… … Wikipedia
Sieve method — Sieve method, or the method of sieves, can mean: in combinatorics, the set of methods dealt with in sieve theory or more specifically, the Inclusion exclusion principle in statistics, and particularly econometrics, the use of sieve estimators in… … Wikipedia
Sieve of Atkin — In mathematics, the sieve of Atkin is a fast, modern algorithm for finding all prime numbers up to a specified integer. It is an optimized version of the ancient sieve of Eratosthenes, but does some preliminary work and then marks off multiples… … Wikipedia
Sieve — In general, a sieve separates wanted/desired elements from unwanted material using a tool such as a mesh, net or other filtration or distillation methods, but it is also used for classification of powders by particle size, or for size measurement … Wikipedia
Eratosthenes of Cyrene — (c. 276 bc–194 bc) Greek astronomer Eratosthenes was born in Cyrene, now in Libya, and educated at Athens. He then taught in Alexandria where he became tutor to the son of Ptolemy III and librarian. He was prominent in history, poetry,… … Scientists