How the sieve of Eratosthenes strikes composites

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·p, p·p+p, p·p+2p, and so on. Multiples below p² were already marked by smaller primes. The algorithm runs in O(n log log n) arithmetic steps and uses O(n) bits. It is a batch prime test, not a primality test for one large integer.


What the problem actually asks

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² is correct because a composite m = a·p with a < p would have been marked when a was processed, or a itself is composite and a smaller prime factor of m already marked it.

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.


What the algorithm guarantees

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.

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.


The working steps

  1. Allocate a boolean array is_composite[0..n], false by default. Mark 0 and 1 as not prime.
  2. For p from 2 to floor(sqrt(n)), if p is still unmarked, mark p·p, p·p+p, p·p+2p, … as composite.
  3. The unmarked numbers from 2 through n are the primes.
  4. Start the inner marking at p·p, not at 2p. Starting at 2p is correct and does redundant writes.
  5. For a range [L, R] with a large L, precompute primes up to sqrt(R) and mark only inside a window of length R−L+1. That is the segmented sieve.

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.


Approaches that look simpler and fail

Approach Why it fails when the inner loop starts at p and not at p squared
Mark multiples of every integer, prime or not 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.
Stop the outer loop at n instead of at sqrt(n) Every composite m ≤ n has a factor at most sqrt(m). The extra outer iterations find no new primes that still have marking work below n.
Use the table as a primality test for numbers larger than n The table has no information past n. A larger integer needs a new sieve or a single-number test.
Expect the marks to name a factor A boolean mark says composite. The smallest prime factor is the linear sieve’s extra array.

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.


A working example

The Bash script and the Python script strike composites from each prime’s square through 30. Both print 2 3 5 7 11 13 17 19 23 29.

n=30
for ((i=0; i<=n; i++)); do prime[i]=1; done
prime[0]=0; prime[1]=0
for ((p=2; p*p<=n; p++)); do
  if (( prime[p] )); then
    for ((m=p*p; m<=n; m+=p)); do prime[m]=0; done
  fi
done
out=()
for ((i=2; i<=n; i++)); do
  if (( prime[i] )); then out+=("$i"); fi
done
echo "${out[*]}"
n = 30
prime = [True] * (n + 1)
prime[0] = prime[1] = False
p = 2
while p * p <= n:
    if prime[p]:
        for m in range(p * p, n + 1, p):
            prime[m] = False
    p += 1
primes = [i for i in range(n + 1) if prime[i]]
assert primes == [2, 3, 5, 7, 11, 13, 17, 19, 23, 29]
print(*primes)

Where it is used

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 Miller-Rabin test and not a sieve.


When you can drop this algorithm

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.


Why this stays published

We publish this so a search for the sieve of Eratosthenes returns the p² start and the O(n log log n) bound. 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’s square.

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

Leave a Reply

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