Professional Documents
Culture Documents
Sieve of Eratosthenes
Sieve of Eratosthenes
Sieve of Eratosthenes: algorithm steps for primes below 121 (including optimization of starting from prime's square).
using trial division to sequentially test each candidate number for divisibility by each
prime. [2]
mathematician.
One of a number of prime number sieves, it is one of the most efficient ways to find all
of the smaller primes. It may be used to find primes in arithmetic progressions. [4]
Contents
1Overview
o 1.1Example
2Algorithm and variants
o 2.1Pseudocode
o 2.2Segmented sieve
o 2.3Incremental sieve
3Computational analysis
4Algorithmic complexity
5Euler's sieve
6See also
7References
8External links
Overview[edit]
Sift the Two's and Sift the Three's,
The Sieve of Eratosthenes.
When the multiples sublime,
The numbers that remain are Prime.
Anonymous[5]
smaller multiples of p will have already been marked at that point. This means that the
algorithm is allowed to terminate in step 4 when p is greater than n.
2 [1]
Another refinement is to initially list odd numbers only, (3, 5, ..., n), and count in increments
of 2p from p in step 3, thus marking only odd multiples of p. This actually appears in the
2
original algorithm. This can be generalized with wheel factorization, forming the initial
[1]
list only from numbers coprime with the first few primes and not just from odds (i.e.,
numbers coprime with 2), and counting in the correspondingly adjusted increments so
that only such multiples of p are generated that are coprime with those small primes, in
the first place. [6]
Example[edit]
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
The first number in the list is 2; cross out every 2nd number in the list after 2 by
counting up from 2 in increments of 2 (these will be all the multiples of 2 in the list):
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
The next number in the list after 2 is 3; cross out every 3rd number in the list after 3 by
counting up from 3 in increments of 3 (these will be all the multiples of 3 in the list):
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
The next number not yet crossed out in the list after 3 is 5; cross out every 5th number
in the list after 5 by counting up from 5 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
The next number not yet crossed out in the list after 5 is 7; the next step would be to
cross out every 7th number in the list after 7, 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 not crossed out at this point in the list are all the prime
numbers below 30:
2 3 5 7 11 13 17 19 23
29
A[j] := false
This algorithm produces all primes not greater than n. It includes a common
optimization, which is to start enumerating the multiples of each prime i from i . The time
2
complexity of this algorithm is O(n log log n), provided the array update is an O(1) operation,
[8]
primes may not fit in memory; worse, even for moderate n, its cache use is highly
suboptimal. The algorithm walks through the entire array A, exhibiting almost no locality
of reference.
A solution to these problems is offered by segmented sieves, where only portions of the
range are sieved at a time. These have been known since the 1970s, and work as
[9]
follows:[8][10]
For ranges with upper limit n so large that the sieving primes below √n as required by the
page segmented sieve of Eratosthenes cannot fit in memory, a slower but much more
space-efficient sieve like the sieve of Sorenson can be used instead. [11]
Incremental sieve[edit]
An incremental formulation of the sieve generates primes indefinitely (i.e., without an
[2]
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). The generation must be initiated only
when the prime's square is reached, to avoid adverse effects on efficiency. It can be
expressed symbolically under the dataflow paradigm as
When testing each prime, the optimal trial division algorithm uses all prime numbers not
exceeding its square root, whereas the sieve of Eratosthenes produces each composite
from its prime factors only, and gets the primes "for free", between the composites. The
widely known 1975 functional sieve code by David Turner is often presented as an
[12]
example of the sieve of Eratosthenes but is actually a sub-optimal trial division sieve.
[6] [2]
Computational analysis[edit]
This section needs additional citations for verification. Please help improve this article by adding citations to reliable
sources. Unsourced material may be challenged and removed. (June 2015) (Learn how and when to remove this template message)
The work performed by this algorithm is almost entirely the operations to cull the
composite number representations which for the basic non-optimized version is the sum
of the range divided by each of the primes up to that range or
not culling until the square means that the sum of the base primes each minus two
is subtracted from the operations. As the sum of the first x primes is 1/2x log x and 2 [13]
of n. The extra offset of two per base prime is 2π(√n), where π is the prime-counting
function in this case, or 4√n/log n; expressing this as a factor of n as are the other terms,
this is 4/√n log n. Combining all of this, the expression for the number of optimized
operations without wheel factorization is
For the wheel factorization cases, there is a further offset of the operations not
done of
where x is the highest wheel prime and a constant factor of the whole
expression is applied which is the fraction of remaining prime candidates as
compared to the repeating wheel circumference. The wheel circumference is
as p − 1/p is the fraction of remaining candidates for the highest wheel
prime, x, and each succeeding smaller prime leaves its corresponding
fraction of the previous combined fraction.
Combining all of the above analysis, the total number of operations
for a sieving range up to n including wheel factorization for primes up
to x is approximately
.
To show that the above expression is a good approximation to the
number of composite number cull operations performed by the
algorithm, following is a table showing the actually measured
number of operations for a practical implementation of the sieve of
Eratosthenes as compared to the number of operations predicted
from the above expression with both expressed as a fraction of
the range (rounded to four decimal places) for different sieve
ranges and wheel factorizations (Note that the last column is a
maximum practical wheel as to the size of the wheel gaps Look
Up Table - almost 10 million values):
Algorithmic complexity[edit]
The sieve of Eratosthenes is a popular way to benchmark
computer performance. As can be seen from the above by
[14]
2/3/5/7/11/13/17/19
n no wheel odds 2/3/5 wheel 2/3/5/7 wheel combo wheel
wheel
1.10 1.07 1.13 1.07 0.290 1.07 0.216 1.26 1.07 0.152 1.41 1.07 1.41 1.07
106 2.122 0.811 1.22 0.114
2 5 7 5 3 5 2 1 5 4 6 5 6 5
2.286 1.07 1.05 0.893 1.10 1.05 0.334 1.15 1.05 0.253 1.17 1.05 0.189 1.24 1.05 0.142 1.24 1.05
107
3 7 9 2 1 9 1 1 9 7 4 9 9 6 9 1 6 9
2.427 1.06 1.04 0.963 1.07 1.04 0.371 1.11 1.04 1.12 1.04 0.222 1.04 0.166 1.04
108 0.286 1.17 1.17
6 2 8 8 9 8 8 3 8 7 8 2 8 2 8
2.551 1.05 1.025 1.06 0.404 1.08 0.314 1.09 0.250 1.12 0.187 1.12
109 1.04 1.04 1.04 1.04 1.04 1.04
4 1 7 4 8 9 3 9 5 7 4 7
101 2.661 1.04 1.03 1.080 1.05 1.03 0.434 1.07 1.03 0.339 1.03 0.275 1.10 1.03 0.206 1.10 1.03
1.08
0
5 3 5 8 4 5 2 3 5 5 5 7 1 5 3 1 5
101 2.760 1.03 1.130 1.04 0.460 1.06 0.362 1.06 0.298 1.08 0.223 1.08
1.03 1.03 1.03 1.03 1.03 1.03
1
8 7 4 6 7 1 2 7 4 2 2 2
101 2.851 1.03 1.02 1.175 1.02 0.484 1.05 1.02 0.382 1.05 1.02 1.06 1.02 0.238 1.06 1.02
1.04 0.319
2
1 3 7 5 7 7 2 7 8 7 7 9 7 7 9 7
101 2.933 1.02 1.02 1.03 1.02 0.506 1.04 1.02 0.401 1.04 1.02 0.337 1.05 1.02 0.252 1.05 1.02
1.217
3
9 9 4 5 4 8 6 4 8 9 4 9 9 4 8 9 4
101 3.010 1.02 1.02 1.255 1.03 1.02 0.527 1.04 1.02 0.419 1.04 1.02 0.355 1.05 1.02 0.265 1.05 1.02
4
4 6 2 2 1 2 2 2 3 4 2 4 2 2 9 2 2
101 3.081 1.02 1.290 1.02 0.546 1.03 0.435 1.03 0.371 1.04 0.278 1.04
1.02 1.02 1.02 1.02 1.02 1.02
5
5 4 7 8 2 6 5 9 7 6 1 6
101 3.147 1.02 1.01 1.323 1.02 1.01 0.563 1.03 1.01 0.450 1.03 1.01 0.386 1.04 1.01 0.289 1.04 1.01
6
8 2 8 9 6 8 9 2 8 7 5 8 8 1 8 4 1 8