<?xml version="1.0"?>
<feed xmlns="http://www.w3.org/2005/Atom" xml:lang="en">
	<id>https://en.bharatpedia.org/w/index.php?action=history&amp;feed=atom&amp;title=Sieve_of_Sundaram</id>
	<title>Sieve of Sundaram - Revision history</title>
	<link rel="self" type="application/atom+xml" href="https://en.bharatpedia.org/w/index.php?action=history&amp;feed=atom&amp;title=Sieve_of_Sundaram"/>
	<link rel="alternate" type="text/html" href="https://en.bharatpedia.org/w/index.php?title=Sieve_of_Sundaram&amp;action=history"/>
	<updated>2026-11-27T16:47:42Z</updated>
	<subtitle>Revision history for this page on the wiki</subtitle>
	<generator>MediaWiki 1.43.6</generator>
	<entry>
		<id>https://en.bharatpedia.org/w/index.php?title=Sieve_of_Sundaram&amp;diff=535777&amp;oldid=prev</id>
		<title>VincentOFarrell: Creating the article w/ infobox...</title>
		<link rel="alternate" type="text/html" href="https://en.bharatpedia.org/w/index.php?title=Sieve_of_Sundaram&amp;diff=535777&amp;oldid=prev"/>
		<updated>2026-04-09T17:56:36Z</updated>

		<summary type="html">&lt;p&gt;Creating the article w/ infobox...&lt;/p&gt;
&lt;p&gt;&lt;b&gt;New page&lt;/b&gt;&lt;/p&gt;&lt;div&gt;{{Short description|Algorithm for generating prime numbers}}&lt;br /&gt;
In [[mathematics]], the &amp;#039;&amp;#039;&amp;#039;sieve of Sundaram&amp;#039;&amp;#039;&amp;#039; is a variant of the [[sieve of Eratosthenes]], a simple [[deterministic algorithm]] for finding all the [[prime number]]s up to a specified integer. It was discovered by [[India]]n student S. P. Sundaram in 1934.&amp;lt;ref&amp;gt;{{cite journal |author=V. Ramaswami Aiyar |title=Sundaram&amp;#039;s Sieve for Prime Numbers |journal=The Mathematics Student |volume=2 |year=1934 |pages=73 |issn=0025-5742 |issue=2}}&amp;lt;/ref&amp;gt;&amp;lt;ref&amp;gt;{{cite journal |author=G.|title=Curiosa 81. A New Sieve for Prime Numbers |journal=[[Scripta Mathematica]] |volume=8 |year=1941 |pages=164 |issue=3}}&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Algorithm==&lt;br /&gt;
[[File:Sieve of Sundaram Animated.gif|right|frame|Sieve of Sundaram: algorithm steps for primes below 202 (unoptimized).]]&lt;br /&gt;
The sieve starts with a list of the integers from 1 to {{Mvar|n}}. From this list, all numbers of the form {{math|&amp;#039;&amp;#039;i&amp;#039;&amp;#039; + &amp;#039;&amp;#039;j&amp;#039;&amp;#039; + 2&amp;#039;&amp;#039;ij&amp;#039;&amp;#039;}} are removed, where {{Mvar|i}} and {{Mvar|j}} are positive integers such that {{Math|1 &amp;amp;le; &amp;#039;&amp;#039;i&amp;#039;&amp;#039; &amp;amp;le; &amp;#039;&amp;#039;j&amp;#039;&amp;#039;}} and {{Math|&amp;#039;&amp;#039;i&amp;#039;&amp;#039; + &amp;#039;&amp;#039;j&amp;#039;&amp;#039; + 2&amp;#039;&amp;#039;ij&amp;#039;&amp;#039; &amp;amp;le; &amp;#039;&amp;#039;n&amp;#039;&amp;#039;.}}  The remaining numbers are doubled and incremented by one, giving a list of the odd prime numbers (that is, all primes except 2) below {{math|2&amp;#039;&amp;#039;n&amp;#039;&amp;#039; + 2.}}&lt;br /&gt;
&lt;br /&gt;
The sieve of Sundaram sieves out the composite numbers just as the [[sieve of Eratosthenes]] does, but even numbers are not considered; the work of &amp;quot;crossing out&amp;quot; the multiples of&amp;amp;nbsp;2 is done by the final double-and-increment step. Whenever Eratosthenes&amp;#039; method would cross out {{Mvar|k}} different multiples of a prime {{Math|2&amp;#039;&amp;#039;i&amp;#039;&amp;#039; + 1}}, Sundaram&amp;#039;s method crosses out {{Math|&amp;#039;&amp;#039;i&amp;#039;&amp;#039; + &amp;#039;&amp;#039;j&amp;#039;&amp;#039;(2&amp;#039;&amp;#039;i&amp;#039;&amp;#039; + 1)}} for {{Math|1 &amp;amp;le; &amp;#039;&amp;#039;j&amp;#039;&amp;#039; &amp;amp;le; {{floor|&amp;#039;&amp;#039;k&amp;#039;&amp;#039;/2}}.}}&lt;br /&gt;
&lt;br /&gt;
==Correctness==&lt;br /&gt;
If we start with integers from {{math|1}} to {{mvar|n}}, the final list contains only odd integers from {{mvar|3}} to {{Math|2&amp;#039;&amp;#039;n&amp;#039;&amp;#039; + 1.}} From this final list, some odd integers have been excluded; we must show these are precisely the &amp;#039;&amp;#039;composite&amp;#039;&amp;#039; odd integers less than {{Math|2&amp;#039;&amp;#039;n&amp;#039;&amp;#039; + 2}}.&lt;br /&gt;
&lt;br /&gt;
Let {{mvar|q}} be an odd integer of the form {{Math|2&amp;#039;&amp;#039;k&amp;#039;&amp;#039; + 1.}} Then, {{mvar|q}} is excluded [[if and only if]] {{mvar|k}} is of the form {{math|&amp;#039;&amp;#039;i&amp;#039;&amp;#039; + &amp;#039;&amp;#039;j&amp;#039;&amp;#039; + 2&amp;#039;&amp;#039;ij&amp;#039;&amp;#039;}}, that is {{math|1=&amp;#039;&amp;#039;q&amp;#039;&amp;#039; = 2(&amp;#039;&amp;#039;i&amp;#039;&amp;#039; + &amp;#039;&amp;#039;j&amp;#039;&amp;#039; + 2&amp;#039;&amp;#039;ij&amp;#039;&amp;#039;) + 1.}} Then {{Math|1=&amp;#039;&amp;#039;q&amp;#039;&amp;#039; = (2&amp;#039;&amp;#039;i&amp;#039;&amp;#039; + 1)(2&amp;#039;&amp;#039;j&amp;#039;&amp;#039; + 1).}}&lt;br /&gt;
&lt;br /&gt;
So, an odd integer is excluded from the final list if and only if it has a factorization of the form {{Math|(2&amp;#039;&amp;#039;i&amp;#039;&amp;#039; + 1)(2&amp;#039;&amp;#039;j&amp;#039;&amp;#039; + 1)}} — which is to say, if it has a non-trivial odd factor. Therefore the list must be composed of exactly the set of odd &amp;#039;&amp;#039;prime&amp;#039;&amp;#039; numbers less than or equal to {{math|2&amp;#039;&amp;#039;n&amp;#039;&amp;#039; + 2.}}&lt;br /&gt;
&lt;br /&gt;
==Asymptotic complexity==&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot; line=&amp;quot;1&amp;quot;&amp;gt;&lt;br /&gt;
def sieve_of_Sundaram(n):&lt;br /&gt;
    k = (n - 2) // 2&lt;br /&gt;
    integers_list = [True] * (k + 1)&lt;br /&gt;
    for i in range(1, k + 1):&lt;br /&gt;
        j = i&lt;br /&gt;
        while i + j + 2 * i * j &amp;lt;= k:&lt;br /&gt;
            integers_list[i + j + 2 * i * j] = False&lt;br /&gt;
            j += 1&lt;br /&gt;
    if n &amp;gt; 2:&lt;br /&gt;
        print(2, end=&amp;quot; &amp;quot;)&lt;br /&gt;
    for i in range(1, k + 1):&lt;br /&gt;
        if integers_list[i]:&lt;br /&gt;
            print(2 * i + 1, end=&amp;quot; &amp;quot;)&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
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:&lt;br /&gt;
&lt;br /&gt;
# The range for the outer {{Tt|i}} 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 {{Math|{{radic|&amp;#039;&amp;#039;n&amp;#039;&amp;#039;}}}}.&lt;br /&gt;
# The code does not properly account for indexing of Python arrays, which are [[Zero-based numbering|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.&lt;br /&gt;
# The inner culling loop (the {{Tt|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.&lt;br /&gt;
&lt;br /&gt;
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 operations:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot; line=&amp;quot;1&amp;quot;&amp;gt;from math import isqrt&lt;br /&gt;
 &lt;br /&gt;
def sieve_of_Sundaram(n):&lt;br /&gt;
    if n &amp;lt; 3:&lt;br /&gt;
        if n &amp;lt; 2:&lt;br /&gt;
            return 0&lt;br /&gt;
        else:&lt;br /&gt;
            return 1    &lt;br /&gt;
    k = (n - 3) // 2 + 1&lt;br /&gt;
&lt;br /&gt;
    integers_list = [True for i in range(k)]&lt;br /&gt;
&lt;br /&gt;
    ops = 0&lt;br /&gt;
&lt;br /&gt;
    for i in range((isqrt(n) - 3) // 2 + 1):&lt;br /&gt;
#        if integers_list[i]: # adding this condition turns it into a SoE!&lt;br /&gt;
            p = 2 * i + 3&lt;br /&gt;
            s = (p * p - 3) // 2 # compute cull start&lt;br /&gt;
&lt;br /&gt;
            for j in range(s, k, p):&lt;br /&gt;
                integers_list[j] = False&lt;br /&gt;
                ops += 1&lt;br /&gt;
&lt;br /&gt;
    print(&amp;quot;Total operations:  &amp;quot;, ops, &amp;quot;;&amp;quot;, sep=&amp;#039;&amp;#039;)&lt;br /&gt;
&lt;br /&gt;
    count = 1&lt;br /&gt;
    for i in range(k):&lt;br /&gt;
        if integers_list[i]:&lt;br /&gt;
            count += 1&lt;br /&gt;
&lt;br /&gt;
    print(&amp;quot;Found &amp;quot;, count, &amp;quot; primes to &amp;quot;, n, &amp;quot;.&amp;quot;, sep=&amp;#039;&amp;#039;)&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
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 &amp;#039;&amp;#039;all odd numbers&amp;#039;&amp;#039; as the base values, whereas the Odds-Only Sieve of Eratosthenes uses &amp;#039;&amp;#039;only the odd primes&amp;#039;&amp;#039; as base values, with both ranges of base values bounded to the square root of the range.&lt;br /&gt;
&lt;br /&gt;
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.&lt;br /&gt;
&lt;br /&gt;
From the above implementation, it is clear that the amount of work done is given by&lt;br /&gt;
&lt;br /&gt;
&amp;lt;math&amp;gt;\int_{a}^{b} \frac{n}{2x} \,dx,&amp;lt;/math&amp;gt;&lt;br /&gt;
&lt;br /&gt;
where {{Mvar|n}} is the range to be sieved and the interval {{Math|[&amp;#039;&amp;#039;a&amp;#039;&amp;#039;, &amp;#039;&amp;#039;b&amp;#039;&amp;#039;]}} is the odd numbers between 3 and {{Math|{{radic|n}}}}.  (The interval {{Math|[&amp;#039;&amp;#039;a&amp;#039;&amp;#039;, &amp;#039;&amp;#039;b&amp;#039;&amp;#039;]}} actually starts at the &amp;#039;&amp;#039;square&amp;#039;&amp;#039; of the odd base values, but this difference is negligible for large ranges.)&lt;br /&gt;
&lt;br /&gt;
As the integral of the reciprocal of {{Math|x}} is exactly {{Math|log(&amp;#039;&amp;#039;x&amp;#039;&amp;#039;)}}, and as the lower value for {{Mvar|a}} is relatively very small (close to 1, whose logarithm is 0), this is about&lt;br /&gt;
&lt;br /&gt;
&amp;lt;math&amp;gt;\frac{n}{8} \log(n).&amp;lt;/math&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Ignoring the constant factor of 1/8, the asymptotic complexity is clearly {{Math|&amp;#039;&amp;#039;O&amp;#039;&amp;#039;(&amp;#039;&amp;#039;n&amp;#039;&amp;#039; log(&amp;#039;&amp;#039;n&amp;#039;&amp;#039;)).}}&lt;br /&gt;
&lt;br /&gt;
==See also==&lt;br /&gt;
* [[Sieve of Eratosthenes]]&lt;br /&gt;
* [[Sieve of Atkin]]&lt;br /&gt;
* [[Sieve theory]]&lt;br /&gt;
&lt;br /&gt;
==References==&lt;br /&gt;
{{reflist}}&lt;br /&gt;
&lt;br /&gt;
==Further reading==&lt;br /&gt;
* {{cite book |last=Ogilvy |first=C. Stanley | authorlink = C. Stanley Ogilvy |author2=John T. Anderson  |title= Excursions in Number Theory |publisher=[[Dover Publications]], 1988 (reprint from [[Oxford University Press]], 1966) |isbn=0-486-25778-9 |url=https://books.google.com/books?isbn=0-486-25778-9 |pages=98–100, 158 |year=1988}}&lt;br /&gt;
* {{cite book |last=Honsberger |first=Ross | author-link=Ross Honsberger|title=Ingenuity in Mathematics |publisher=[[Mathematical Association of America]] |series=New Mathematical Library #23 |year=1970 |isbn=0-394-70923-3 |pages=[https://archive.org/details/ingenuityinmathe0000hons/page/75 75] |url=https://archive.org/details/ingenuityinmathe0000hons/page/75 }}&lt;br /&gt;
* {{cite journal |last=Movshovitz-Hadar |first=N. |year=1988 |title=Stimulating Presentations of Theorems Followed by Responsive Proofs |journal=For the Learning of Mathematics |volume=8 |issue=2 |pages=12–19}}&lt;br /&gt;
* {{cite thesis |type=PhD |last=Ferrando |first=Elisabetta |title=Abductive processes in conjecturing and proving |publisher=Purdue University |year=2005 |pages=70–72 |url=https://web.inge.unige.it/SMA/Sv/AbPCP.pdf}}&lt;br /&gt;
* {{cite journal |last=Baxter |first=Andrew |title=Sundaram&amp;#039;s Sieve |journal=Topics from the History of Cryptography |publisher=MU Department of Mathematics |url=http://banach.millersville.edu/~bob/math478/History/Sundaram.html}} {{dead link|date=April 2026}}&lt;br /&gt;
&lt;br /&gt;
==External links==&lt;br /&gt;
* [http://xojoc.pw/dailyprogrammer/sieve-sundaram.html A C99 implementation of the Sieve of Sundaram using bitarrays]&lt;br /&gt;
&lt;br /&gt;
{{number theoretic algorithms}}&lt;br /&gt;
&lt;br /&gt;
[[Category:Primality tests]]&lt;br /&gt;
[[Category:Indian inventions]]&lt;/div&gt;</summary>
		<author><name>VincentOFarrell</name></author>
	</entry>
</feed>