Katana VentraIP

Sieve of Eratosthenes

In mathematics, the sieve of Eratosthenes is an ancient algorithm for finding all prime numbers up to any given limit.

For the sculpture, see The Sieve of Eratosthenes (sculpture).

It does so by iteratively marking as composite (i.e., not prime) the multiples of each prime, starting with the first prime number, 2. The multiples of a given prime are generated as a sequence of numbers starting from that prime, with constant difference between them that is equal to that prime.[1] This is the sieve's key distinction from using trial division to sequentially test each candidate number for divisibility by each prime.[2] Once all the multiples of each discovered prime have been marked as composites, the remaining unmarked numbers are primes.


The earliest known reference to the sieve (Ancient Greek: κόσκινον Ἐρατοσθένους, kóskinon Eratosthénous) is in Nicomachus of Gerasa's Introduction to Arithmetic,[3] an early 2nd cent. CE book which attributes it to Eratosthenes of Cyrene, a 3rd cent. BCE Greek mathematician, though describing the sieving by odd numbers instead of by primes.[4]


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.[5]

Algorithm and variants[edit]

Pseudocode[edit]

The sieve of Eratosthenes can be expressed in pseudocode, as follows:[8][9]

Algorithmic complexity[edit]

The sieve of Eratosthenes is a popular way to benchmark computer performance.[14] The time complexity of calculating all primes below n in the random access machine model is O(n log log n) operations, a direct consequence of the fact that the prime harmonic series asymptotically approaches log log n. It has an exponential time complexity with regard to length of the input, though, which makes it a pseudo-polynomial algorithm. The basic algorithm requires O(n) of memory.


The bit complexity of the algorithm is O(n (log n) (log log n)) bit operations with a memory requirement of O(n).[15]


The normally implemented page segmented version has the same operational complexity of O(n log log n) as the non-segmented version but reduces the space requirements to the very minimal size of the segment page plus the memory required to store the base primes less than the square root of the range used to cull composites from successive page segments of size O(n/log n).


A special (rarely, if ever, implemented) segmented version of the sieve of Eratosthenes, with basic optimizations, uses O(n) operations and O(nlog log n/log n) bits of memory.[16][17][18]


Using big O notation ignores constant factors and offsets that may be very significant for practical ranges: The sieve of Eratosthenes variation known as the Pritchard wheel sieve[16][17][18] has an O(n) performance, but its basic implementation requires either a "one large array" algorithm which limits its usable range to the amount of available memory else it needs to be page segmented to reduce memory use. When implemented with page segmentation in order to save memory, the basic algorithm still requires about O(n/log n) bits of memory (much more than the requirement of the basic page segmented sieve of Eratosthenes using O(n/log n) bits of memory). Pritchard's work reduced the memory requirement at the cost of a large constant factor. Although the resulting wheel sieve has O(n) performance and an acceptable memory requirement, it is not faster than a reasonably Wheel Factorized basic sieve of Eratosthenes for practical sieving ranges.

Sieve of Pritchard

Sieve of Atkin

Sieve of Sundaram

Sieve theory

primesieve – Very fast highly optimized C/C++ segmented Sieve of Eratosthenes

Eratosthenes, sieve of at Encyclopaedia of Mathematics

Interactive JavaScript Page

by George Beck, Wolfram Demonstrations Project.

Sieve of Eratosthenes

Sieve of Eratosthenes in Haskell

Sieve of Eratosthenes algorithm illustrated and explained. Java and C++ implementations.

A related sieve written in x86 assembly language

Fast optimized highly parallel CUDA segmented Sieve of Eratosthenes in C

SieveOfEratosthenesInManyProgrammingLanguages c2 wiki page

Sieve of Eratosthenes in C from 1998 with nice features and algorithmic tricks explained.

The Art of Prime Sieving