Sieve Of Sundaram
   HOME

TheInfoList



OR:

In
mathematics Mathematics is a field of study that discovers and organizes methods, Mathematical theory, theories and theorems that are developed and Mathematical proof, proved for the needs of empirical sciences and mathematics itself. There are many ar ...
, 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 number, composite (i.e., not prime) the multiples of each prime, starting with ...
, 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 fa ...
for finding all the
prime number A prime number (or a prime) is a natural number greater than 1 that is not a Product (mathematics), 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 ...
s up to a specified integer. It was discovered by
India India, officially the Republic of India, is a country in South Asia. It is the List of countries and dependencies by area, seventh-largest country by area; the List of countries by population (United Nations), most populous country since ...
n student S. P. Sundaram in 1934.


Algorithm

The sieve starts with a list of the integers from 1 to . From this list, all numbers of the form are removed, where and are positive integers such that and The remaining numbers are doubled and incremented by one, giving a list of the odd prime numbers (that is, 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 number, composite (i.e., not prime) the multiples of each prime, starting with ...
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 different multiples of a prime , Sundaram's method crosses out for


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" (often shortened as "iff") is paraphrased by the biconditional, a logical connective between statements. The biconditional is true in two cases, where either bo ...
is of the form , that is Then 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


Asymptotic complexity

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 the genus '' Ruta'' grown as an ornamental plant and herb. It is native to the Mediterranean. It is grown throughout the world in gardens, especially for i ...
* (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=" ")
The above obscure-but-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 looping variable is much too large, resulting in redundant looping that cannot perform any composite number culling; the proper range is to the array indices that represent odd numbers less than . # The code does not properly account for indexing of Python arrays, which are zero-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 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 In mathematics, the prime-counting function is the function counting the number of prime numbers less than or equal to some real number . It is denoted by (unrelated to the number ). A symmetric variant seen sometimes is , which is equal ...
that also displays the total number of composite-culling operations: from math import isqrt 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((isqrt(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='') The commented-out line is all that is necessary to convert the Sieve of Sundaram to the Odds-Only 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 given by \int_^ \frac \,dx, where is the range to be sieved and the interval is the odd numbers between 3 and . (The interval 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 is exactly , and as the lower value for is relatively very small (close to 1, whose logarithm is 0), this is about \frac \log(n). Ignoring the constant factor of 1/8, the asymptotic complexity is clearly


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 number, composite (i.e., not prime) the multiples of each prime, starting with ...
* Sieve of Atkin *
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 limi ...


References


Further reading

* * * * *


External links


A C99 implementation of the Sieve of Sundaram using bitarrays
{{number theoretic algorithms Primality tests Indian inventions