HOME

TheInfoList



OR:

In
mathematics Mathematics is an area of knowledge that includes the topics of numbers, formulas and related structures, shapes and the spaces in which they are contained, and quantities and their changes. These topics are represented in modern mathematics ...
, the sieve of Sundaram is a variant of the
sieve of Eratosthenes In mathematics, the sieve of Eratosthenes is an ancient algorithm for finding all prime numbers up to any given limit. It does so by iteratively marking as composite (i.e., not prime) the multiples of each prime, starting with the first prime n ...
, a simple
deterministic algorithm In computer science, a deterministic algorithm is an algorithm that, given a particular input, will always produce the same output, with the underlying machine always passing through the same sequence of states. Deterministic algorithms are by far ...
for finding all the
prime number A prime number (or a prime) is a natural number greater than 1 that is not a product of two smaller natural numbers. A natural number greater than 1 that is not prime is called a composite number. For example, 5 is prime because the only ways ...
s up to a specified integer. It was discovered by
India India, officially the Republic of India (Hindi: ), is a country in South Asia. It is the seventh-largest country by area, the second-most populous country, and the most populous democracy in the world. Bounded by the Indian Ocean on the so ...
n student S. P. Sundaram in 1934.


Algorithm

Start with a list of the integers from 1 to ''n''. From this list, remove all numbers of the form where: *i,j\in\mathbb,\ 1 \le i \le j *i + j + 2ij \le n The remaining numbers are doubled and incremented by one, giving a list of the odd prime numbers (i.e., all primes except 2) below . The sieve of Sundaram sieves out the composite numbers just as the
sieve of Eratosthenes In mathematics, the sieve of Eratosthenes is an ancient algorithm for finding all prime numbers up to any given limit. It does so by iteratively marking as composite (i.e., not prime) the multiples of each prime, starting with the first prime n ...
does, but even numbers are not considered; the work of "crossing out" the multiples of 2 is done by the final double-and-increment step. Whenever Eratosthenes' method would cross out ''k'' different multiples of a prime , Sundaram's method crosses out for 1\le j\le \lfloor k/2\rfloor.


Correctness

If we start with integers from to , the final list contains only odd integers from to . From this final list, some odd integers have been excluded; we must show these are precisely the ''composite'' odd integers less than . Let be an odd integer of the form . Then, is excluded
if and only if In logic and related fields such as mathematics and philosophy, "if and only if" (shortened as "iff") is a biconditional logical connective between statements, where either both statements are true or both are false. The connective is bicondi ...
is of the form , that is . Then we have: : \begin q &= 2(i + j + 2ij) + 1 \\ &= 2i + 2j + 4ij + 1 \\ &= (2i + 1)(2j + 1). \end So, an odd integer is excluded from the final list if and only if it has a factorization of the form — which is to say, if it has a non-trivial odd factor. Therefore the list must be composed of exactly the set of odd ''prime'' numbers less than or equal to . def sieve_of_Sundaram(n): """The sieve of Sundaram is a simple deterministic algorithm for finding all the prime numbers up to a specified integer.""" k = (n - 2) // 2 integers_list =
rue ''Ruta graveolens'', commonly known as rue, common rue or herb-of-grace, is a species of ''Ruta'' grown as an ornamental plant and herb. It is native to the Balkan Peninsula. It is grown throughout the world in gardens, especially for its bluis ...
* (k + 1) for i in range(1, k + 1): j = i while i + j + 2 * i * j <= k: integers_list + j + 2 * i * j= False j += 1 if n > 2: print(2, end=' ') for i in range(1, k + 1): if integers_list print(2 * i + 1, end=' ')


Asymptotic Complexity

The above obscure but as commonly implemented Python version of the Sieve of Sundaram hides the true complexity of the algorithm due to the following reasons: # The range for the outer ''i'' looping variable is much too large, resulting in redundant looping that can't perform any composite number representation culling; the proper range is to the array index represent odd numbers less than the square root of the range. # The code doesn't properly account for indexing of Python arrays, which are zero index based so that it ignores the values at the bottom and top of the array; this is a minor issue, but serves to show that the algorithm behind the code has not been clearly understood. # The inner culling loop (the ''j'' loop) exactly reflects the way the algorithm is formulated, but seemingly without realizing that the indexed culling starts at exactly the index representing the square of the base odd number and that the indexing using multiplication can much more easily be expressed as a simple repeated addition of the base odd number across the range; in fact, this method of adding a constant value across the culling range is exactly how the Sieve of Eratosthenes culling is generally implemented. The following Python code in the same style resolves the above three issues, as well converting the code to a prime counting function that also displays the total number of composite culling representation culling operations: import math def sieve_of_Sundaram(n): """The sieve of Sundaram is a simple deterministic algorithm for finding all the prime numbers up to a specified integer.""" if n < 3: if n < 2: return 0 else: return 1 k = (n - 3) // 2 + 1 integers_list = rue for i in range(k) ops = 0 for i in range((int(math.sqrt(n)) - 3) // 2 + 1): # if integers_list # adding this condition turns it into a SoE! p = 2 * i + 3 s = (p * p - 3) // 2 # compute cull start for j in range(s, k, p): integers_list = False ops += 1 print("Total operations: ", ops, ";", sep='') count = 1 for i in range(k): if integers_list count += 1 print("Found ", count, " primes to ", n, ".", sep='') Note the commented out line which is all that is necessary to convert the Sieve of Sundaram to the Odds-Only (wheel factorized by the only even prime of two) Sieve of Eratosthenes; this clarifies that the only difference between these two algorithms is that the Sieve of Sundaram culls composite numbers using all odd numbers as the base values, whereas the Odds-Only Sieve of Eratosthenes uses only the odd primes as base values, with both ranges of base values bounded to the square root of the range. When run for various ranges, it is immediately clear that while, of course, the resulting count of primes for a given range is identical between the two algorithms, the number of culling operations is much higher for the Sieve of Sundaram and also grows much more quickly with increasing range. From the above implementation, it is clear that the amount of work done is according to the following:
\int_^ \frac \,dx. or \frac \int_^ \frac \,dx. where: * n is the range to be sieved and * the range ''a'' to ''b'' is the odd numbers between 3 and the square root of ''n'' - the ''a'' to ''b'' range actually starts at the square of the odd base values, but this difference is negligible for large ranges. As the integral of the reciprocal of ''x'' is exactly \log, and as the lower value for ''a'' is relatively very small (close to one which has a ''log'' value of zero), this is about as follows:
\frac \log or \frac \frac \log or \frac \log. Ignoring the constant factor of one eighth, the asymptotic complexity in
Big O notation Big ''O'' notation is a mathematical notation that describes the limiting behavior of a function when the argument tends towards a particular value or infinity. Big O is a member of a family of notations invented by Paul Bachmann, Edmund Lan ...
is clearly O( \log).


See also

*
Sieve of Eratosthenes In mathematics, the sieve of Eratosthenes is an ancient algorithm for finding all prime numbers up to any given limit. It does so by iteratively marking as composite (i.e., not prime) the multiples of each prime, starting with the first prime n ...
*
Sieve of Atkin In mathematics, the sieve of Atkin is a modern algorithm for finding all prime numbers up to a specified integer. Compared with the ancient sieve of Eratosthenes, which marks off multiples of primes, the sieve of Atkin does some preliminary work a ...
*
Sieve theory 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 prototypical example of a sifted set is the set of prime numbers up to some prescribed lim ...


References

* *
A new "sieve" for primes
an excerpt from (translation of Russian book ) * * *


External links



{{number theoretic algorithms Primality tests