How the linear sieve marks each composite once

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·i, 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.


What the problem actually asks

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·p only for primes p up to that smallest factor. The composite i·p then sees p as its smallest prime factor, and no later prime will mark it.

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.


What the algorithm guarantees

After one pass, lp[x] is the smallest prime factor of x for every x ≥ 2, and the collected list is exactly the primes up to n. Time is Θ(n) and memory is Θ(n) integers. Factoring any x ≤ 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.

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’s rho is the tool for one large composite.


The working steps

  1. Allocate lp[0..n] as 0 and an empty prime list.
  2. 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.
  3. For each prime p in the list, if p·i would exceed n, stop the prime loop. Set lp[p·i] = p.
  4. If p equals lp[i], stop the prime loop. Do not mark i times a larger prime in this iteration.
  5. To factor x, while x > 1 emit lp[x] and divide x by lp[x].

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.


Approaches that look simpler and fail

Approach Why it fails when the prime loop does not stop at the smallest factor
Mark i·p for every prime p with i·p ≤ n 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.
Set lp[i·p] = i The value stored must be a prime factor of the composite. i may be composite. The algorithm stores p.
Use lp to factor a number larger than n The array has no entry there. Trial division or Pollard’s rho is required.
Forget to set lp[i] = i when i is prime Later iterations need the smallest prime factor of a prime to be itself, both for the factorisation walk and for the stop condition.

A bitset of primality is Eratosthenes. One large factorisation is Pollard’s rho or a stronger factorisation algorithm. The linear sieve is the O(n) smallest-prime-factor table.


A working example

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.

n=30
for ((i=0; i<=n; i++)); do lp[i]=0; done
pc=0
for ((i=2; i<=n; i++)); do
  if (( lp[i] == 0 )); then
    lp[i]=$i
    primes[pc]=$i
    pc=$((pc+1))
  fi
  for ((k=0; k<pc; k++)); do
    p=${primes[k]}
    if (( p > lp[i] || i * p > n )); then break; fi
    lp[i*p]=$p
  done
done
echo "${primes[*]}"
n = 30
lp = [0] * (n + 1)
primes = []
for i in range(2, n + 1):
    if lp[i] == 0:
        lp[i] = i
        primes.append(i)
    for p in primes:
        if p > lp[i] or i * p > n:
            break
        lp[i * p] = p
assert primes == [2, 3, 5, 7, 11, 13, 17, 19, 23, 29]
print(*primes)

Where it is used

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.


When you can drop this algorithm

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.


Why this stays published

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. The linear sieve fills a smallest-prime-factor table up to n in linear time by marking each composite exactly once.

本文由 HoHo 與 AI 協作整理,最後更新於 2026 年 11 月 13 日。

Leave a Reply

Your email address will not be published. Required fields are marked *