{"id":25768,"date":"2017-10-25T20:22:45","date_gmt":"2017-10-25T14:52:45","guid":{"rendered":"https:\/\/www.wikitechy.com\/technology\/?p=25768"},"modified":"2017-10-25T20:22:45","modified_gmt":"2017-10-25T14:52:45","slug":"c-programming-sieve-eratosthenes","status":"publish","type":"post","link":"https:\/\/www.wikitechy.com\/technology\/c-programming-sieve-eratosthenes\/","title":{"rendered":"C programming &#8211; Sieve of Eratosthenes"},"content":{"rendered":"<p>Given a number n, print all primes smaller than or equal to n. It is also given that n is a small number.<br \/>\nFor example, if n is 10, the output should be \u201c2, 3, 5, 7\u201d. If n is 20, the output should be \u201c2, 3, 5, 7, 11, 13, 17, 19\u201d.<\/p>\n<p>The sieve of Eratosthenes is one of the most efficient ways to find all primes smaller than n when n is smaller than 10 million<\/p>\n<div>\n<p>Following is the algorithm to find all the prime numbers less than or equal to a given integer\u00a0<em>n<\/em>\u00a0by Eratosthenes\u2019 method:<\/p>\n<ol>\n<li>Create a list of consecutive integers from 2 to\u00a0<em>n<\/em>: (2, 3, 4, \u2026,\u00a0<em>n<\/em>).<\/li>\n<li>Initially, let\u00a0<em>p<\/em>\u00a0equal 2, the first prime number.<\/li>\n<li>Starting from\u00a0<em>p<\/em>, count up in increments of\u00a0<em>p<\/em>\u00a0and mark each of these numbers greater than\u00a0<em>p<\/em>\u00a0itself in the list. These numbers will be 2<em>p<\/em>, 3<em>p<\/em>, 4<em>p<\/em>, etc.; note that some of them may have already been marked.<\/li>\n<li>Find the first number greater than\u00a0<em>p<\/em>\u00a0in the list that is not marked. If there was no such number, stop. Otherwise, let\u00a0<em>p<\/em>\u00a0now equal this number (which is the next prime), and repeat from step 3.<\/li>\n<\/ol>\n<p>When the algorithm terminates, all the numbers in the list that are not marked are prime.<\/p>\n[ad type=&#8221;banner&#8221;]\n<\/div>\n<p><strong>Explanation with Example:<\/strong><br \/>\nLet us take an example when n = 50. So we need to print all print numbers smaller than or equal to 50.<\/p>\n<p>We create a list of all numbers from 2 to 50<\/p>\n<p><img fetchpriority=\"high\" decoding=\"async\" class=\"aligncenter size-full wp-image-25775\" src=\"https:\/\/www.wikitechy.com\/technology\/wp-content\/uploads\/2017\/05\/Sieve1-660x115.png\" alt=\"sieve1\" width=\"660\" height=\"115\" srcset=\"https:\/\/www.wikitechy.com\/technology\/wp-content\/uploads\/2017\/05\/Sieve1-660x115.png 660w, https:\/\/www.wikitechy.com\/technology\/wp-content\/uploads\/2017\/05\/Sieve1-660x115-300x52.png 300w\" sizes=\"(max-width: 660px) 100vw, 660px\" \/><\/p>\n<p>According to the algorithm we will mark all the numbers which are divisible by 2.<\/p>\n<p><img decoding=\"async\" class=\"aligncenter size-full wp-image-25776\" src=\"https:\/\/www.wikitechy.com\/technology\/wp-content\/uploads\/2017\/05\/sieve2.png\" alt=\"\" width=\"1024\" height=\"177\" srcset=\"https:\/\/www.wikitechy.com\/technology\/wp-content\/uploads\/2017\/05\/sieve2.png 1024w, https:\/\/www.wikitechy.com\/technology\/wp-content\/uploads\/2017\/05\/sieve2-300x52.png 300w, https:\/\/www.wikitechy.com\/technology\/wp-content\/uploads\/2017\/05\/sieve2-768x133.png 768w, https:\/\/www.wikitechy.com\/technology\/wp-content\/uploads\/2017\/05\/sieve2-990x171.png 990w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><\/p>\n<p>Now we move to our next unmarked number 3 and mark all the numbers which are multiples of 3.<\/p>\n<p><img decoding=\"async\" class=\"aligncenter size-full wp-image-25777\" src=\"https:\/\/www.wikitechy.com\/technology\/wp-content\/uploads\/2017\/05\/sieve3.png\" alt=\"\" width=\"1178\" height=\"204\" srcset=\"https:\/\/www.wikitechy.com\/technology\/wp-content\/uploads\/2017\/05\/sieve3.png 1178w, https:\/\/www.wikitechy.com\/technology\/wp-content\/uploads\/2017\/05\/sieve3-300x52.png 300w, https:\/\/www.wikitechy.com\/technology\/wp-content\/uploads\/2017\/05\/sieve3-768x133.png 768w, https:\/\/www.wikitechy.com\/technology\/wp-content\/uploads\/2017\/05\/sieve3-1024x177.png 1024w, https:\/\/www.wikitechy.com\/technology\/wp-content\/uploads\/2017\/05\/sieve3-990x171.png 990w\" sizes=\"(max-width: 1178px) 100vw, 1178px\" \/><\/p>\n<p>We move to our next unmarked number 5 and mark all multiples of 5.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-25778\" src=\"https:\/\/www.wikitechy.com\/technology\/wp-content\/uploads\/2017\/05\/Sieve4.png\" alt=\"\" width=\"1024\" height=\"178\" srcset=\"https:\/\/www.wikitechy.com\/technology\/wp-content\/uploads\/2017\/05\/Sieve4.png 1024w, https:\/\/www.wikitechy.com\/technology\/wp-content\/uploads\/2017\/05\/Sieve4-300x52.png 300w, https:\/\/www.wikitechy.com\/technology\/wp-content\/uploads\/2017\/05\/Sieve4-768x134.png 768w, https:\/\/www.wikitechy.com\/technology\/wp-content\/uploads\/2017\/05\/Sieve4-990x172.png 990w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><\/p>\n<p>We continue this process and our final table will look like below:<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-25779\" src=\"https:\/\/www.wikitechy.com\/technology\/wp-content\/uploads\/2017\/05\/Sieve5.png\" alt=\"\" width=\"1246\" height=\"220\" srcset=\"https:\/\/www.wikitechy.com\/technology\/wp-content\/uploads\/2017\/05\/Sieve5.png 1246w, https:\/\/www.wikitechy.com\/technology\/wp-content\/uploads\/2017\/05\/Sieve5-300x53.png 300w, https:\/\/www.wikitechy.com\/technology\/wp-content\/uploads\/2017\/05\/Sieve5-768x136.png 768w, https:\/\/www.wikitechy.com\/technology\/wp-content\/uploads\/2017\/05\/Sieve5-1024x181.png 1024w, https:\/\/www.wikitechy.com\/technology\/wp-content\/uploads\/2017\/05\/Sieve5-990x175.png 990w\" sizes=\"(max-width: 1246px) 100vw, 1246px\" \/><\/p>\n<p>So the prime numbers are the unmarked ones: 2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47.<\/p>\n[ad type=&#8221;banner&#8221;]\n<p><strong>Implementation:<\/strong><br \/>\nFollowing is C++ implementation of the above algorithm. In the following implementation, a boolean array arr[] of size n is used to mark multiples of prime numbers.<\/p>\n<div class=\"code-embed-wrapper\"> <div class=\"code-embed-infos\"> <span class=\"code-embed-name\">C Program<\/span> <\/div> <pre class=\"language-c code-embed-pre line-numbers\"  data-start=\"1\" data-line-offset=\"0\"><code class=\"language-c code-embed-code\">\/\/ C++ program to print all primes smaller than or equal to<br\/>\/\/ n using Sieve of Eratosthenes<br\/>#include &lt;bits\/stdc++.h&gt;<br\/>using namespace std;<br\/> <br\/>void SieveOfEratosthenes(int n)<br\/>{<br\/>    \/\/ Create a boolean array &quot;prime[0..n]&quot; and initialize<br\/>    \/\/ all entries it as true. A value in prime[i] will<br\/>    \/\/ finally be false if i is Not a prime, else true.<br\/>    bool prime[n+1];<br\/>    memset(prime, true, sizeof(prime));<br\/> <br\/>    for (int p=2; p*p&lt;=n; p++)<br\/>    {<br\/>        \/\/ If prime[p] is not changed, then it is a prime<br\/>        if (prime[p] == true)<br\/>        {<br\/>            \/\/ Update all multiples of p<br\/>            for (int i=p*2; i&lt;=n; i += p)<br\/>                prime[i] = false;<br\/>        }<br\/>    }<br\/> <br\/>    \/\/ Print all prime numbers<br\/>    for (int p=2; p&lt;=n; p++)<br\/>       if (prime[p])<br\/>          cout &lt;&lt; p &lt;&lt; &quot; &quot;;<br\/>}<br\/> <br\/>\/\/ Driver Program to test above function<br\/>int main()<br\/>{<br\/>    int n = 30;<br\/>    cout &lt;&lt; &quot;Following are the prime numbers smaller &quot;<br\/>         &lt;&lt; &quot; than or equal to &quot; &lt;&lt; n &lt;&lt; endl;<br\/>    SieveOfEratosthenes(n);<br\/>    return 0;<br\/>}<\/code><\/pre> <\/div>\n<p><strong>Output:<\/strong><\/p>\n<pre>Following are the prime numbers below 30\r\n2 3 5 7 11 13 17 19 23 29<\/pre>\n[ad type=&#8221;banner&#8221;]\n","protected":false},"excerpt":{"rendered":"<p>C programming &#8211; Sieve of Eratosthenes &#8211; Mathematical Algorithms &#8211; Given a number n, print all primes smaller than or equal to n. if n is 10, the output .<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[69866,1,74058],"tags":[75218,75210,75231,75205,75206,75204,75221,75209,75192,75227,75200,75193,75195,72110,70565,75215,75223,74082,75212,75199,75229,75208,75216,75225,75226,75222,75228,72118,75217,75230],"class_list":["post-25768","post","type-post","status-publish","format-standard","hentry","category-c-programming","category-coding","category-mathematical-algorithms","tag-define-sieve-of-eratosthenes","tag-eratosthenes-method","tag-eratosthenes-sieve-java","tag-finding-prime-numbers","tag-how-to-find-prime-numbers","tag-n-prime","tag-number-4-sieve","tag-prime-no-in-c","tag-prime-no-program-in-c","tag-prime-no-program-in-c-language","tag-prime-number-algorithm","tag-prime-number-generation-program-in-c","tag-prime-number-program-in-c-language","tag-prime-number-program-in-c","tag-prime-numbers","tag-prime-numbers-from-1-to-100-in-c","tag-prime-series-in-c","tag-program-for-prime-number-in-c","tag-program-to-find-prime-numbers","tag-sieve","tag-sieve-c","tag-sieve-method-to-find-prime-numbers","tag-sieve-number","tag-sieve-number-4","tag-sieve-of-eratosthenes-algorithm-in-c","tag-sieve-prime","tag-sieving-method","tag-simple-c-program-for-prime-number","tag-simple-prime-number-program-in-c-language","tag-the-sieve"],"_links":{"self":[{"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/posts\/25768","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/comments?post=25768"}],"version-history":[{"count":0,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/posts\/25768\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/media?parent=25768"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/categories?post=25768"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/tags?post=25768"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}