{"id":281,"date":"2026-11-13T12:36:49","date_gmt":"2026-11-13T04:36:49","guid":{"rendered":"https:\/\/hoho.live\/luda\/index.php\/2026\/11\/13\/how-the-linear-sieve-marks-each-composite-once\/"},"modified":"2026-11-13T21:58:27","modified_gmt":"2026-11-13T13:58:27","slug":"how-the-linear-sieve-marks-each-composite-once","status":"publish","type":"post","link":"https:\/\/hoho.live\/luda\/index.php\/2026\/11\/13\/how-the-linear-sieve-marks-each-composite-once\/","title":{"rendered":"How the linear sieve marks each composite once"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\"><strong>Short answer:<\/strong> The <strong>linear sieve<\/strong> computes the smallest prime factor of every integer up to n in <strong>O(n)<\/strong> arithmetic steps. It walks i from 2 to n, records i as prime when it has no factor yet, and for each known prime p marks p\u00b7i, stopping when p divides i or p exceeds the smallest prime factor already stored. Each composite is written exactly once, by its smallest prime factor. The prime list and the factor array are the output.<\/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\">Eratosthenes rewrites a composite once for every distinct prime factor, and the harmonic sum of those rewrites is the log log factor. Storing the smallest prime factor allows the loop to mark i\u00b7p only for primes p up to that smallest factor. The composite i\u00b7p then sees p as its smallest prime factor, and no later prime will mark it.<\/p>\n\n\n<p class=\"wp-block-paragraph\">The early stop, when p divides i, is what keeps the write unique. Omitting it marks some numbers more than once and can also store a factor that is not smallest, depending on the order.<\/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 one pass, lp[x] is the smallest prime factor of x for every x \u2265 2, and the collected list is exactly the primes up to n. Time is \u0398(n) and memory is \u0398(n) integers. Factoring any x \u2264 n is then repeated division by lp[x], in time proportional to the number of prime factors with multiplicity. That is the usual reason to keep the array instead of a boolean sieve.<\/p>\n\n\n<p class=\"wp-block-paragraph\">The linear bound counts array operations, not bit complexity of huge integers. It is an enumeration algorithm up to n. It does not factor a single integer larger than n. Pollard&#8217;s rho is the tool for one large composite.<\/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 lp[0..n] as 0 and an empty prime list.<\/li>\n<li>For i from 2 to n, if lp[i] is 0, then i is prime: set lp[i] = i and append i to the prime list.<\/li>\n<li>For each prime p in the list, if p\u00b7i would exceed n, stop the prime loop. Set lp[p\u00b7i] = p.<\/li>\n<li>If p equals lp[i], stop the prime loop. Do not mark i times a larger prime in this iteration.<\/li>\n<li>To factor x, while x &gt; 1 emit lp[x] and divide x by lp[x].<\/li>\n<\/ol>\n\n\n<p class=\"wp-block-paragraph\">The linear bound is the enumeration. A boolean Eratosthenes sieve uses less memory per entry and is often faster when only primality is required. The linear sieve pays an integer per cell in exchange for the smallest factor and the single-write property. Neither sieve replaces a primality test at cryptographic sizes.<\/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 prime loop does not stop at the smallest factor<\/th>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td>Mark i\u00b7p for every prime p with i\u00b7p \u2264 n<\/td>\n<td>Some composites are written many times, and the stored factor may be whichever prime ran last rather than the smallest. The complexity returns to the Eratosthenes shape.<\/td>\n<\/tr>\n<tr>\n<td>Set lp[i\u00b7p] = i<\/td>\n<td>The value stored must be a prime factor of the composite. i may be composite. The algorithm stores p.<\/td>\n<\/tr>\n<tr>\n<td>Use lp to factor a number larger than n<\/td>\n<td>The array has no entry there. Trial division or Pollard&#8217;s rho is required.<\/td>\n<\/tr>\n<tr>\n<td>Forget to set lp[i] = i when i is prime<\/td>\n<td>Later iterations need the smallest prime factor of a prime to be itself, both for the factorisation walk and for the stop condition.<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<\/figure>\n\n\n<p class=\"wp-block-paragraph\">A bitset of primality is Eratosthenes. One large factorisation is Pollard&#8217;s rho or a stronger factorisation algorithm. The linear sieve is the O(n) smallest-prime-factor table.<\/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 mark each composite once, by its smallest prime factor. The inner loop stops when p equals that factor. Both print the primes through 30.<\/p>\n\n<pre><code class=\"language-bash\">n=30\nfor ((i=0; i&lt;=n; i++)); do lp[i]=0; done\npc=0\nfor ((i=2; i&lt;=n; i++)); do\n  if (( lp[i] == 0 )); then\n    lp[i]=$i\n    primes[pc]=$i\n    pc=$((pc+1))\n  fi\n  for ((k=0; k&lt;pc; k++)); do\n    p=${primes[k]}\n    if (( p &gt; lp[i] || i * p &gt; n )); then break; fi\n    lp[i*p]=$p\n  done\ndone\necho &quot;${primes[*]}&quot;\n<\/code><\/pre>\n<pre><code class=\"language-python\">n = 30\nlp = [0] * (n + 1)\nprimes = []\nfor i in range(2, n + 1):\n    if lp[i] == 0:\n        lp[i] = i\n        primes.append(i)\n    for p in primes:\n        if p &gt; lp[i] or i * p &gt; n:\n            break\n        lp[i * p] = p\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 linear sieve writes the smallest prime factor of every integer up to a bound, which is how a program factors many small integers in linear time. The sieve of Eratosthenes only strikes composites and is the usual prime table. Both are tables, not a primality test of one large integer.<\/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 the linear sieve when a bitset of primes is enough and memory is tight. Use Eratosthenes. Keep the linear sieve when every integer up to n must be factored or must report its smallest prime factor.<\/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 linear sieve returns the early stop at the smallest prime factor and the O(n) single write per composite. <strong>The linear sieve fills a smallest-prime-factor table up to n in linear time by marking each composite exactly once.<\/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 linear sieve computes the smallest prime factor of every integer up to n in O(n) arithmetic steps. It walks i from 2 to n, records i as prime when it has no factor yet, and for each known prime p marks p\u00b7i, stopping when p divides&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-281","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\/281","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=281"}],"version-history":[{"count":2,"href":"https:\/\/hoho.live\/luda\/index.php\/wp-json\/wp\/v2\/posts\/281\/revisions"}],"predecessor-version":[{"id":680,"href":"https:\/\/hoho.live\/luda\/index.php\/wp-json\/wp\/v2\/posts\/281\/revisions\/680"}],"wp:attachment":[{"href":"https:\/\/hoho.live\/luda\/index.php\/wp-json\/wp\/v2\/media?parent=281"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/hoho.live\/luda\/index.php\/wp-json\/wp\/v2\/categories?post=281"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/hoho.live\/luda\/index.php\/wp-json\/wp\/v2\/tags?post=281"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}