{"id":14,"date":"2011-08-23T02:48:52","date_gmt":"2011-08-23T02:48:52","guid":{"rendered":"http:\/\/learninglover.com\/blog\/?p=14"},"modified":"2016-12-15T08:15:21","modified_gmt":"2016-12-15T13:15:21","slug":"sieve-of-eratosthenes","status":"publish","type":"post","link":"https:\/\/learninglover.com\/blog\/index.php\/2011\/08\/23\/sieve-of-eratosthenes\/","title":{"rendered":"Sieve of Eratosthenes"},"content":{"rendered":"<p>Prime numbers are an important concept in Number Theory and Cryptography which often uses the difficulty of finding prime numbers as a basis for building encryption systems that are difficult to break without going through all (or a very large number of) possible choices. <\/p>\n<p>Remember that a prime number is a number greater than 1 whose only divisors are 1 and that number itself. One of the most famous algorithms for searching for prime numbers is the Sieve of Eratosthenes. I added a script which implements the Sieve of Eratosthenes to my Examples page. <\/p>\n<p>This algorithm prints out all prime numbers less than a given number by first canceling out all multiples of 2 (the smallest prime), then all multiples of 3 (the second smallest prime), then all multiples of 5 (the third smallest prime &#8211; multiples of 4 do not need to be considered because they are also multiples of 2), etc until we have reached a number which cannot be a divisor of this maximum number.<\/p>\n<p>So if we are given a number, n, the first step of the algorithm is write out a table that lists all the numbers that are less than n. For example lets run this Sieve on 50. So all numbers less than 50 are<\/p>\n<p>1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50. <\/p>\n<p>So since 1 is not a prime number (by the definition of prime numbers), we cancel that number out. <\/p>\n<p><del datetime=\"2013-11-05T22:01:11+00:00\">1<\/del>, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50. <\/p>\n<p>Next, we look at the list and the first number that is not crossed out is a prime. That number is 2. We will put a mark by 2 and cancel out all of 2&#8217;s multiples. <\/p>\n<p><del datetime=\"2013-11-05T22:01:11+00:00\">1<\/del>, <strong>2<\/strong>*, 3, <del datetime=\"2013-11-05T22:01:11+00:00\">4<\/del>, 5, <del datetime=\"2013-11-05T22:01:11+00:00\">6<\/del>, 7, <del datetime=\"2013-11-05T22:01:11+00:00\">8<\/del>, 9, <del datetime=\"2013-11-05T22:01:11+00:00\">10<\/del>, 11, <del datetime=\"2013-11-05T22:01:11+00:00\">12<\/del>, 13, <del datetime=\"2013-11-05T22:01:11+00:00\">14<\/del>, 15, <del datetime=\"2013-11-05T22:01:11+00:00\">16<\/del>, 17, <del datetime=\"2013-11-05T22:01:11+00:00\">18<\/del>, 19, <del datetime=\"2013-11-05T22:01:11+00:00\">20<\/del>, 21, <del datetime=\"2013-11-05T22:01:11+00:00\">22<\/del>, 23, <del datetime=\"2013-11-05T22:01:11+00:00\">24<\/del>, 25, <del datetime=\"2013-11-05T22:01:11+00:00\">26<\/del>, 27, <del datetime=\"2013-11-05T22:01:11+00:00\">28<\/del>, 29, <del datetime=\"2013-11-05T22:01:11+00:00\">30<\/del>, 31, <del datetime=\"2013-11-05T22:01:11+00:00\">32<\/del>, 33, <del datetime=\"2013-11-05T22:01:11+00:00\">34<\/del>, 35, <del datetime=\"2013-11-05T22:01:11+00:00\">36<\/del>, 37, <del datetime=\"2013-11-05T22:01:11+00:00\">38<\/del>, 39, <del datetime=\"2013-11-05T22:01:11+00:00\">40<\/del>, 41, <del datetime=\"2013-11-05T22:01:11+00:00\">42<\/del>, 43, <del datetime=\"2013-11-05T22:01:11+00:00\">44<\/del>, 45, <del datetime=\"2013-11-05T22:01:11+00:00\">46<\/del>, 47, <del datetime=\"2013-11-05T22:01:11+00:00\">48<\/del>, 49, <del datetime=\"2013-11-05T22:01:11+00:00\">50<\/del>. <\/p>\n<p>Again, we look at the list and the first number that is not marked or crossed out is 3, so that number is prime. We will put a mark by 3 and cancel out all of 3&#8217;s multiples. <\/p>\n<p><del datetime=\"2013-11-05T22:01:11+00:00\">1<\/del>, <strong>2<\/strong>*, <strong>3<\/strong>*, <del datetime=\"2013-11-05T22:01:11+00:00\">4<\/del>, 5, <del datetime=\"2013-11-05T22:01:11+00:00\">6<\/del>, 7, <del datetime=\"2013-11-05T22:01:11+00:00\">8<\/del>, <del datetime=\"2013-11-05T22:01:11+00:00\">9<\/del>, <del datetime=\"2013-11-05T22:01:11+00:00\">10<\/del>, 11, <del datetime=\"2013-11-05T22:01:11+00:00\">12<\/del>, 13, <del datetime=\"2013-11-05T22:01:11+00:00\">14<\/del>, <del datetime=\"2013-11-05T22:01:11+00:00\">15<\/del>, <del datetime=\"2013-11-05T22:01:11+00:00\">16<\/del>, 17, <del datetime=\"2013-11-05T22:01:11+00:00\">18<\/del>, 19, <del datetime=\"2013-11-05T22:01:11+00:00\">20<\/del>, <del datetime=\"2013-11-05T22:01:11+00:00\">21<\/del>, <del datetime=\"2013-11-05T22:01:11+00:00\">22<\/del>, 23, <del datetime=\"2013-11-05T22:01:11+00:00\">24<\/del>, 25, <del datetime=\"2013-11-05T22:01:11+00:00\">26<\/del>, <del datetime=\"2013-11-05T22:01:11+00:00\">27<\/del>, <del datetime=\"2013-11-05T22:01:11+00:00\">28<\/del>, 29, <del datetime=\"2013-11-05T22:01:11+00:00\">30<\/del>, 31, <del datetime=\"2013-11-05T22:01:11+00:00\">32<\/del>, <del datetime=\"2013-11-05T22:01:11+00:00\">33<\/del>, <del datetime=\"2013-11-05T22:01:11+00:00\">34<\/del>, 35, <del datetime=\"2013-11-05T22:01:11+00:00\">36<\/del>, 37, <del datetime=\"2013-11-05T22:01:11+00:00\">38<\/del>, <del datetime=\"2013-11-05T22:01:11+00:00\">39<\/del>, <del datetime=\"2013-11-05T22:01:11+00:00\">40<\/del>, 41, <del datetime=\"2013-11-05T22:01:11+00:00\">42<\/del>, 43, <del datetime=\"2013-11-05T22:01:11+00:00\">44<\/del>, <del datetime=\"2013-11-05T22:01:11+00:00\">45<\/del>, <del datetime=\"2013-11-05T22:01:11+00:00\">46<\/del>, 47, <del datetime=\"2013-11-05T22:01:11+00:00\">48<\/del>, 49, <del datetime=\"2013-11-05T22:01:11+00:00\">50<\/del>. <\/p>\n<p>Once again, we look at the list and the first number that is not marked or crossed out is 5, so that number is prime. We will put a mark by 5 and cancel out all of 5&#8217;s multiples. <\/p>\n<p><del datetime=\"2013-11-05T22:01:11+00:00\">1<\/del>, <strong>2<\/strong>*, <strong>3<\/strong>*, <del datetime=\"2013-11-05T22:01:11+00:00\">4<\/del>, <strong>5<\/strong>*, <del datetime=\"2013-11-05T22:01:11+00:00\">6<\/del>, 7, <del datetime=\"2013-11-05T22:01:11+00:00\">8<\/del>, <del datetime=\"2013-11-05T22:01:11+00:00\">9<\/del>, <del datetime=\"2013-11-05T22:01:11+00:00\">10<\/del>, 11, <del datetime=\"2013-11-05T22:01:11+00:00\">12<\/del>, 13, <del datetime=\"2013-11-05T22:01:11+00:00\">14<\/del>, <del datetime=\"2013-11-05T22:01:11+00:00\">15<\/del>, <del datetime=\"2013-11-05T22:01:11+00:00\">16<\/del>, 17, <del datetime=\"2013-11-05T22:01:11+00:00\">18<\/del>, 19, <del datetime=\"2013-11-05T22:01:11+00:00\">20<\/del>, <del datetime=\"2013-11-05T22:01:11+00:00\">21<\/del>, <del datetime=\"2013-11-05T22:01:11+00:00\">22<\/del>, 23, <del datetime=\"2013-11-05T22:01:11+00:00\">24<\/del>, <del datetime=\"2013-11-05T22:01:11+00:00\">25<\/del>, <del datetime=\"2013-11-05T22:01:11+00:00\">26<\/del>, <del datetime=\"2013-11-05T22:01:11+00:00\">27<\/del>, <del datetime=\"2013-11-05T22:01:11+00:00\">28<\/del>, 29, <del datetime=\"2013-11-05T22:01:11+00:00\">30<\/del>, 31, <del datetime=\"2013-11-05T22:01:11+00:00\">32<\/del>, <del datetime=\"2013-11-05T22:01:11+00:00\">33<\/del>, <del datetime=\"2013-11-05T22:01:11+00:00\">34<\/del>, <del datetime=\"2013-11-05T22:01:11+00:00\">35<\/del>, <del datetime=\"2013-11-05T22:01:11+00:00\">36<\/del>, 37, <del datetime=\"2013-11-05T22:01:11+00:00\">38<\/del>, <del datetime=\"2013-11-05T22:01:11+00:00\">39<\/del>, <del datetime=\"2013-11-05T22:01:11+00:00\">40<\/del>, 41, <del datetime=\"2013-11-05T22:01:11+00:00\">42<\/del>, 43, <del datetime=\"2013-11-05T22:01:11+00:00\">44<\/del>, <del datetime=\"2013-11-05T22:01:11+00:00\">45<\/del>, <del datetime=\"2013-11-05T22:01:11+00:00\">46<\/del>, 47, <del datetime=\"2013-11-05T22:01:11+00:00\">48<\/del>, 49, <del datetime=\"2013-11-05T22:01:11+00:00\">50<\/del>. <\/p>\n<p>We look at the list and the first number that is not marked or crossed out is 7, so that number is prime. We will put a mark by 7 and cancel out all of 7&#8217;s multiples. <\/p>\n<p><del datetime=\"2013-11-05T22:01:11+00:00\">1<\/del>, <strong>2<\/strong>*, <strong>3<\/strong>*, <del datetime=\"2013-11-05T22:01:11+00:00\">4<\/del>, <strong>5<\/strong>*, <del datetime=\"2013-11-05T22:01:11+00:00\">6<\/del>, <strong>7<\/strong>*, <del datetime=\"2013-11-05T22:01:11+00:00\">8<\/del>, <del datetime=\"2013-11-05T22:01:11+00:00\">9<\/del>, <del datetime=\"2013-11-05T22:01:11+00:00\">10<\/del>, 11, <del datetime=\"2013-11-05T22:01:11+00:00\">12<\/del>, 13, <del datetime=\"2013-11-05T22:01:11+00:00\">14<\/del>, <del datetime=\"2013-11-05T22:01:11+00:00\">15<\/del>, <del datetime=\"2013-11-05T22:01:11+00:00\">16<\/del>, 17, <del datetime=\"2013-11-05T22:01:11+00:00\">18<\/del>, 19, <del datetime=\"2013-11-05T22:01:11+00:00\">20<\/del>, <del datetime=\"2013-11-05T22:01:11+00:00\">21<\/del>, <del datetime=\"2013-11-05T22:01:11+00:00\">22<\/del>, 23, <del datetime=\"2013-11-05T22:01:11+00:00\">24<\/del>, <del datetime=\"2013-11-05T22:01:11+00:00\">25<\/del>, <del datetime=\"2013-11-05T22:01:11+00:00\">26<\/del>, <del datetime=\"2013-11-05T22:01:11+00:00\">27<\/del>, <del datetime=\"2013-11-05T22:01:11+00:00\">28<\/del>, 29, <del datetime=\"2013-11-05T22:01:11+00:00\">30<\/del>, 31, <del datetime=\"2013-11-05T22:01:11+00:00\">32<\/del>, <del datetime=\"2013-11-05T22:01:11+00:00\">33<\/del>, <del datetime=\"2013-11-05T22:01:11+00:00\">34<\/del>, <del datetime=\"2013-11-05T22:01:11+00:00\">35<\/del>, <del datetime=\"2013-11-05T22:01:11+00:00\">36<\/del>, 37, <del datetime=\"2013-11-05T22:01:11+00:00\">38<\/del>, <del datetime=\"2013-11-05T22:01:11+00:00\">39<\/del>, <del datetime=\"2013-11-05T22:01:11+00:00\">40<\/del>, 41, <del datetime=\"2013-11-05T22:01:11+00:00\">42<\/del>, 43, <del datetime=\"2013-11-05T22:01:11+00:00\">44<\/del>, <del datetime=\"2013-11-05T22:01:11+00:00\">45<\/del>, <del datetime=\"2013-11-05T22:01:11+00:00\">46<\/del>, 47, <del datetime=\"2013-11-05T22:01:11+00:00\">48<\/del>, <del datetime=\"2013-11-05T22:01:11+00:00\">49<\/del>, <del datetime=\"2013-11-05T22:01:11+00:00\">50<\/del>. <\/p>\n<p>Now, we look and the first number that is not crossed out is 11. However, since 11 is greater than sqrt(50) we know that each of 11&#8217;s multiples that are less than 50 will have been cancelled out by a previous prime number. So we have finished the algorithm. <\/p>\n<p>Check out <a href=\"http:\/\/www.learninglover.com\/examples.php?id=3\">my script which implements the Sieve of Eratosthenes<a> for more examples. <\/p>\n","protected":false},"excerpt":{"rendered":"<p>Prime numbers are an important concept in Number Theory and Cryptography which often uses the difficulty of finding prime numbers as a basis for building encryption systems that are difficult to break without going through all (or a very large number of) possible choices. Remember that a prime number is a number greater than 1 [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[],"tags":[],"class_list":["post-14","post","type-post","status-publish","format-standard","hentry"],"_links":{"self":[{"href":"https:\/\/learninglover.com\/blog\/index.php\/wp-json\/wp\/v2\/posts\/14","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/learninglover.com\/blog\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/learninglover.com\/blog\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/learninglover.com\/blog\/index.php\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/learninglover.com\/blog\/index.php\/wp-json\/wp\/v2\/comments?post=14"}],"version-history":[{"count":6,"href":"https:\/\/learninglover.com\/blog\/index.php\/wp-json\/wp\/v2\/posts\/14\/revisions"}],"predecessor-version":[{"id":1311,"href":"https:\/\/learninglover.com\/blog\/index.php\/wp-json\/wp\/v2\/posts\/14\/revisions\/1311"}],"wp:attachment":[{"href":"https:\/\/learninglover.com\/blog\/index.php\/wp-json\/wp\/v2\/media?parent=14"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/learninglover.com\/blog\/index.php\/wp-json\/wp\/v2\/categories?post=14"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/learninglover.com\/blog\/index.php\/wp-json\/wp\/v2\/tags?post=14"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}