{"id":284,"date":"2026-11-13T12:36:55","date_gmt":"2026-11-13T04:36:55","guid":{"rendered":"https:\/\/hoho.live\/luda\/index.php\/2026\/11\/13\/how-the-sieve-of-eratosthenes-strikes-composites\/"},"modified":"2026-11-13T21:58:31","modified_gmt":"2026-11-13T13:58:31","slug":"how-the-sieve-of-eratosthenes-strikes-composites","status":"publish","type":"post","link":"https:\/\/hoho.live\/luda\/index.php\/2026\/11\/13\/how-the-sieve-of-eratosthenes-strikes-composites\/","title":{"rendered":"How the sieve of Eratosthenes strikes composites"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\"><strong>Short answer:<\/strong> The <strong>sieve of Eratosthenes<\/strong> lists every prime up to n by striking multiples. For each prime p whose square is still at most n, it marks p\u00b7p, p\u00b7p+p, p\u00b7p+2p, and so on. Multiples below p\u00b2 were already marked by smaller primes. The algorithm runs in <strong>O(n log log n)<\/strong> arithmetic steps and uses <strong>O(n)<\/strong> bits. It is a batch prime test, not a primality test for one large integer.<\/p>\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n<h4 class=\"wp-block-heading\"><strong>What the problem actually asks<\/strong><\/h4>\n\n<p class=\"wp-block-paragraph\">Testing each integer up to n by trial division repeats work: every composite is divided by primes that a shared table could have applied once. The sieve marks a multiple when it first knows a factor. Starting the inner loop at p\u00b2 is correct because a composite m = a\u00b7p with a &lt; p would have been marked when a was processed, or a itself is composite and a smaller prime factor of m already marked it.<\/p>\n\n\n<p class=\"wp-block-paragraph\">The practical bottleneck on large n is memory, not the log log factor. A segmented sieve runs the same marking over successive windows so the bitset stays in cache.<\/p>\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n<h4 class=\"wp-block-heading\"><strong>What the algorithm guarantees<\/strong><\/h4>\n\n<p class=\"wp-block-paragraph\">After termination, every unmarked index greater than or equal to 2 is prime, and every composite up to n is marked. Time is O(n log log n) for the classic loop, which is O(n) for every practical n because log log n grows so slowly. Extra memory is n bits. The algorithm does not factor the composites it marks unless the smallest prime factor is stored beside the mark. That upgrade is the linear sieve.<\/p>\n\n\n<p class=\"wp-block-paragraph\">A single number the size of a cryptographic modulus is not a sieve input. Miller-Rabin or a deterministic variant tests one odd number. The sieve builds a table.<\/p>\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n<h4 class=\"wp-block-heading\"><strong>The working steps<\/strong><\/h4>\n\n<ol class=\"wp-block-list\">\n<li>Allocate a boolean array is_composite[0..n], false by default. Mark 0 and 1 as not prime.<\/li>\n<li>For p from 2 to floor(sqrt(n)), if p is still unmarked, mark p\u00b7p, p\u00b7p+p, p\u00b7p+2p, \u2026 as composite.<\/li>\n<li>The unmarked numbers from 2 through n are the primes.<\/li>\n<li>Start the inner marking at p\u00b7p, not at 2p. Starting at 2p is correct and does redundant writes.<\/li>\n<li>For a range [L, R] with a large L, precompute primes up to sqrt(R) and mark only inside a window of length R\u2212L+1. That is the segmented sieve.<\/li>\n<\/ol>\n\n\n<p class=\"wp-block-paragraph\">The bound is for all primes up to n. One primality test of a large integer is Miller-Rabin or a deterministic test such as AKS, which is not how anyone tests a 64-bit number in practice. The linear sieve stores smallest prime factors and runs in O(n). Eratosthenes does not need that array.<\/p>\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n<h4 class=\"wp-block-heading\"><strong>Approaches that look simpler and fail<\/strong><\/h4>\n\n<figure class=\"wp-block-table\">\n<table>\n<thead>\n<tr>\n<th>Approach<\/th>\n<th>Why it fails when the inner loop starts at p and not at p squared<\/th>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td>Mark multiples of every integer, prime or not<\/td>\n<td>The result is still correct and the time is worse. Only unmarked p need to mark. A composite p was itself struck by a smaller prime.<\/td>\n<\/tr>\n<tr>\n<td>Stop the outer loop at n instead of at sqrt(n)<\/td>\n<td>Every composite m \u2264 n has a factor at most sqrt(m). The extra outer iterations find no new primes that still have marking work below n.<\/td>\n<\/tr>\n<tr>\n<td>Use the table as a primality test for numbers larger than n<\/td>\n<td>The table has no information past n. A larger integer needs a new sieve or a single-number test.<\/td>\n<\/tr>\n<tr>\n<td>Expect the marks to name a factor<\/td>\n<td>A boolean mark says composite. The smallest prime factor is the linear sieve&#8217;s extra array.<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<\/figure>\n\n\n<p class=\"wp-block-paragraph\">Smallest prime factors in O(n) are the linear sieve. One large integer is Miller-Rabin. Eratosthenes is the boolean table of primes up to n.<\/p>\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n<h4 class=\"wp-block-heading\"><strong>A working example<\/strong><\/h4>\n\n<p class=\"wp-block-paragraph\">The Bash script and the Python script strike composites from each prime&#8217;s square through 30. Both print <code>2 3 5 7 11 13 17 19 23 29<\/code>.<\/p>\n\n<pre><code class=\"language-bash\">n=30\nfor ((i=0; i&lt;=n; i++)); do prime[i]=1; done\nprime[0]=0; prime[1]=0\nfor ((p=2; p*p&lt;=n; p++)); do\n  if (( prime[p] )); then\n    for ((m=p*p; m&lt;=n; m+=p)); do prime[m]=0; done\n  fi\ndone\nout=()\nfor ((i=2; i&lt;=n; i++)); do\n  if (( prime[i] )); then out+=(&quot;$i&quot;); fi\ndone\necho &quot;${out[*]}&quot;\n<\/code><\/pre>\n<pre><code class=\"language-python\">n = 30\nprime = [True] * (n + 1)\nprime[0] = prime[1] = False\np = 2\nwhile p * p &lt;= n:\n    if prime[p]:\n        for m in range(p * p, n + 1, p):\n            prime[m] = False\n    p += 1\nprimes = [i for i in range(n + 1) if prime[i]]\nassert primes == [2, 3, 5, 7, 11, 13, 17, 19, 23, 29]\nprint(*primes)\n<\/code><\/pre>\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n<h4 class=\"wp-block-heading\"><strong>Where it is used<\/strong><\/h4>\n\n<p class=\"wp-block-paragraph\">The sieve of Eratosthenes builds the prime tables used by enumerations, by small-prime trial division, and by segmented sieves at larger bounds. A single large probable prime, as in RSA key generation, is a <strong>Miller-Rabin<\/strong> test and not a sieve.<\/p>\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n<h4 class=\"wp-block-heading\"><strong>When you can drop this algorithm<\/strong><\/h4>\n\n<p class=\"wp-block-paragraph\">Drop a full sieve when a single integer near cryptographic size must be tested. Use Miller-Rabin. Keep the sieve, segmented if needed, to enumerate primes or to answer many primality queries up to n.<\/p>\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n<h4 class=\"wp-block-heading\"><strong>Why this stays published<\/strong><\/h4>\n\n<p class=\"wp-block-paragraph\">We publish this so a search for the sieve of Eratosthenes returns the p\u00b2 start and the O(n log log n) bound. <strong>The sieve of Eratosthenes marks composites up to n in O(n log log n) time by striking multiples of each prime, starting at that prime&#8217;s square.<\/strong><\/p>\n\n\n<p class=\"wp-block-paragraph\">\u672c\u6587\u7531 HoHo \u8207 AI \u5354\u4f5c\u6574\u7406\uff0c\u6700\u5f8c\u66f4\u65b0\u65bc 2026 \u5e74 11 \u6708 13 \u65e5\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Short answer: The sieve of Eratosthenes lists every prime up to n by striking multiples. For each prime p whose square is still at most n, it marks p\u00b7p, p\u00b7p+p, p\u00b7p+2p, and so on. Multiples below p\u00b2 were already marked by smaller primes. The algorithm runs in O(n log&hellip;<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[30,42],"tags":[],"class_list":["post-284","post","type-post","status-publish","format-standard","hentry","category-algorithms","category-number-theory"],"_links":{"self":[{"href":"https:\/\/hoho.live\/luda\/index.php\/wp-json\/wp\/v2\/posts\/284","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/hoho.live\/luda\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/hoho.live\/luda\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/hoho.live\/luda\/index.php\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/hoho.live\/luda\/index.php\/wp-json\/wp\/v2\/comments?post=284"}],"version-history":[{"count":2,"href":"https:\/\/hoho.live\/luda\/index.php\/wp-json\/wp\/v2\/posts\/284\/revisions"}],"predecessor-version":[{"id":683,"href":"https:\/\/hoho.live\/luda\/index.php\/wp-json\/wp\/v2\/posts\/284\/revisions\/683"}],"wp:attachment":[{"href":"https:\/\/hoho.live\/luda\/index.php\/wp-json\/wp\/v2\/media?parent=284"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/hoho.live\/luda\/index.php\/wp-json\/wp\/v2\/categories?post=284"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/hoho.live\/luda\/index.php\/wp-json\/wp\/v2\/tags?post=284"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}