MathLabs

Arithmetic and number theory

Sieve methods

Techniques that estimate how many integers in a range survive after removing multiples of small primes.

IntuitionFiltering out the multiples, one prime at a time

Picture a grid of numbers from 2 to 100. Cross out every multiple of 2 except 2 itself. Then every multiple of 3 except 3. Then every multiple of 5 except 5. Keep going with each surviving number, and whatever is left uncrossed at the end is exactly the primes. This ancient filtering process — a "sieve" — is not just a way to list primes; sieve methods turn the same idea into a precise counting tool, estimating how many integers in a range survive any given filter, even when listing them one by one is computationally hopeless.

A Riemann-sum widget with n bars, used as a visual analogy for how a sieve's running count $S(A,\mathcal{P},z)$ shrinks as more prime filters are applied; it is not an actual sieve computation, since this widget's preset content is a numerical-integration demo, not a divisor filter.
Sieve of Eratosthenes on 1..601..60: slide nn to cross out multiples of primes ≤n\le n (red); the surviving green cells are primes.

SchoolThe Sieve of Eratosthenes

Definition: Sieve of Eratosthenes

To list every prime up to NN: write down 2,3,…,N2,3,\ldots,N; repeatedly take the smallest unmarked number pp, declare it prime, and mark every multiple of pp (starting from p2p^2) as composite; stop once p2>Np^2 > N.

P(z)=∏p<zpP(z) = \prod_{p < z} p

Why stop at p2>Np^2>N? Any composite n≤Nn\le N has a prime factor ≤N\le\sqrt N (otherwise its two smallest prime factors would multiply to more than NN), so once every prime up to N\sqrt N has done its marking, everything left unmarked genuinely has no factor to expose it — it must be prime. The product P(z)=∏p<zpP(z) = \prod_{p < z} p, over all primes p<zp<z, is exactly the set of "small" primes the sieve filters against; sieve theory calls it the level of the sieve.

Sieving [2, 30] step by step
Filter appliedNewly marked compositeRemaining unmarked count
p = 2 (from 4)4,6,8,...,30 (14 numbers)29 − 14 = 15
p = 3 (from 9)9,15,21,27 (4 new)15 − 4 = 11
p = 5 (from 25)25 (1 new)11 − 1 = 10
Stop: 7² = 49 > 30—10 primes: 2,3,5,7,11,13,17,19,23,29

UndergraduateTwo theorems: why the sieve works, and how to count with it

An integer n≥2n \ge 2 is prime if and only if it is not divisible by any prime p≤np \le \sqrt n.

Why is it true?

This equivalence is exactly what lets the sieve stop early: once every prime up to N\sqrt N has crossed off its multiples, there is no possible witness left to convict any remaining number of being composite, so testing (or crossing off) larger primes is provably wasted work.

Proof

(⇐\Leftarrow) Suppose nn has no prime factor ≤n\le\sqrt n. If nn were composite, write n=abn=ab with 1<a≤b<n1<a\le b<n. Then a≤na\le\sqrt n (otherwise a>na>\sqrt n and b≥a>nb\ge a>\sqrt n would force ab>nab>n, a contradiction), and aa's smallest prime factor pp satisfies p≤a≤np\le a\le\sqrt n, so pp is a prime factor of nn that is ≤n\le\sqrt n — contradicting the hypothesis. So nn has no such factorization, hence nn is prime.

(⇒\Rightarrow) If nn is prime, its only positive divisors are 11 and nn; no prime p<np<n divides it at all, so in particular none with p≤np\le\sqrt n does either.

Together these show the two conditions are equivalent, which is exactly the termination criterion used in the sieve: after processing every prime up to N\sqrt N, everything still unmarked in [2,N][2,N] satisfies the right-hand side, hence is prime.

For A={1,2,…,x}A=\{1,2,\ldots,x\} and P\mathcal P the set of primes p<zp<z, the count of a∈Aa \in A coprime to P(z)=∏p<zpP(z)=\prod_{p<z}p is S(A,P,z)=∑d∣P(z)μ(d)⌊xd⌋\displaystyle S(A,\mathcal P,z)=\sum_{d \mid P(z)} \mu(d)\Big\lfloor \dfrac{x}{d} \Big\rfloor, where μ\mu is the Möbius function; taking z=x+1z=\sqrt x+1 gives π(x)−π(x)+1=S(A,P,x)=∑d∣P(x)μ(d) ⌊x/d⌋\pi(x)-\pi(\sqrt{x})+1 = S(A,\mathcal{P},\sqrt{x}) = \sum_{d \mid P(\sqrt{x})} \mu(d)\, \lfloor x/d \rfloor.

Why is it true?

This is the precise, quantitative version of "cross off multiples": instead of physically marking a grid, it counts survivors directly by inclusion–exclusion over which small primes divide them. It is the ancestor of every modern sieve (Brun, Selberg, the large sieve, GPY) used to attack twin primes and bounded gaps — all of them are, at bottom, smarter ways of controlling the error terms that this exact formula produces.

Proof

Every integer a∈Aa\in A is coprime to P(z)P(z) iff it is divisible by none of the primes p<zp<z. For each divisor d∣P(z)d\mid P(z) (a squarefree product of some subset of these primes), the count of multiples of dd in A={1,…,x}A=\{1,\ldots,x\} is exactly ⌊x/d⌋\lfloor x/d\rfloor.

By inclusion–exclusion over the events "p∣ap\mid a" for p<zp<z: the count of a∈Aa\in A divisible by at least one prime in P\mathcal P is ∑p<z⌊x/p⌋−∑p<q⌊x/(pq)⌋+∑p<q<r⌊x/(pqr)⌋−⋯\sum_{p<z}\lfloor x/p\rfloor - \sum_{p<q}\lfloor x/(pq)\rfloor + \sum_{p<q<r}\lfloor x/(pqr)\rfloor - \cdots, alternating with the number of primes multiplied together. Each such alternating sign is exactly the Möbius function μ(d)\mu(d) of the corresponding squarefree d∣P(z)d\mid P(z): μ(1)=1\mu(1)=1, μ(d)=(−1)k\mu(d)=(-1)^k for dd a product of kk distinct primes.

So the count divisible by at least one prime in P\mathcal P is −∑d∣P(z), d>1μ(d)⌊x/d⌋-\sum_{d\mid P(z),\,d>1}\mu(d)\lfloor x/d\rfloor. Subtracting this from ∣A∣=⌊x⌋=x|A|=\lfloor x\rfloor=x (the d=1d=1 term, μ(1)⌊x/1⌋=x\mu(1)\lfloor x/1\rfloor=x) gives the count coprime to P(z)P(z): S(A,P,z)=x−∑d∣P(z),d>1μ(d)⌊x/d⌋=∑d∣P(z)μ(d)⌊x/d⌋S(A,\mathcal P,z)=x-\sum_{d\mid P(z),d>1}\mu(d)\lfloor x/d\rfloor=\sum_{d\mid P(z)}\mu(d)\lfloor x/d\rfloor.

Finally, taking z=x+1z=\sqrt x+1 so that P\mathcal P is exactly the primes ≤x\le\sqrt x: any a∈[2,x]a\in[2,x] coprime to all of them is either 11 or a prime >x>\sqrt x (by the theorem above, since it has no prime factor ≤x\le\sqrt x), so S(A,P,x+1)=1+(π(x)−π(x))S(A,\mathcal P,\sqrt x+1)=1+\big(\pi(x)-\pi(\sqrt x)\big), giving the stated identity.

π(x)−π(x)+1=S(A,P,x)=∑d∣P(x)μ(d) ⌊x/d⌋\pi(x)-\pi(\sqrt{x})+1 = S(A,\mathcal{P},\sqrt{x}) = \sum_{d \mid P(\sqrt{x})} \mu(d)\, \lfloor x/d \rfloor

UndergraduateReal-World Applications and Worked Examples

Sieve methods power both practical computation and deep pure-math breakthroughs. In software, segmented versions of Eratosthenes's sieve are the standard algorithm for enumerating primes at scale, and a quick small-prime sieve is the universal first pass before running expensive primality tests (like Miller–Rabin) in RSA key generation — throwing away ~80% of random odd candidates in microseconds. In pure mathematics, refined sieves (Brun, Selberg, GPY, Maynard–Tao) are the engine behind every modern breakthrough on prime gaps.

Example: Counting primes up to 30 with Legendre's formula

Apply Legendre's formula with x=30x=30 and z=6z=6 (so the sieving primes are 2,3,52,3,5, since 30≈5.48\sqrt{30}\approx 5.48) to compute π(30)\pi(30) from scratch.

Solution

Here P(6)=2⋅3⋅5=30P(6)=2\cdot3\cdot5=30, whose 23=82^3=8 squarefree divisors are 1,2,3,5,6,10,15,301,2,3,5,6,10,15,30.

Evaluate μ(d)⌊30/d⌋\mu(d)\lfloor30/d\rfloor for each: +⌊30/1⌋=30+\lfloor30/1\rfloor=30; −⌊30/2⌋−⌊30/3⌋−⌊30/5⌋=−15−10−6=−31-\lfloor30/2\rfloor-\lfloor30/3\rfloor-\lfloor30/5\rfloor=-15-10-6=-31; +⌊30/6⌋+⌊30/10⌋+⌊30/15⌋=+5+3+2=+10+\lfloor30/6\rfloor+\lfloor30/10\rfloor+\lfloor30/15\rfloor=+5+3+2=+10; −⌊30/30⌋=−1-\lfloor30/30\rfloor=-1.

Summing gives S(A,P,6)=30−31+10−1=8S(A,\mathcal P,6)=30-31+10-1=8. By Legendre's identity π(30)−π(30)+1=8\pi(30)-\pi(\sqrt{30})+1=8, and π(30)=π(5)=3\pi(\sqrt{30})=\pi(5)=3 (the primes 2,3,52,3,5), so π(30)=8+3−1=10\pi(30)=8+3-1=10 — matching the 1010 primes listed in the table above.

Example: How much work does a small-prime pre-sieve save in RSA keygen?

Before running an expensive Miller–Rabin test on a random odd candidate, an RSA library first checks whether it is divisible by 3,5,7,11,133,5,7,11,13. What fraction of random odd integers survives this 5-prime pre-sieve?

Solution

By the Chinese Remainder Theorem, residue classes modulo distinct primes 3,5,7,11,133,5,7,11,13 (and 22, already fixed to odd) are independent over a full period 2⋅3⋅5⋅7⋅11⋅13=30,0302\cdot3\cdot5\cdot7\cdot11\cdot13=30{,}030.

A random odd integer is not divisible by pp with probability 1−1/p1-1/p, so the fraction surviving all five filters is (1−1/3)(1−1/5)(1−1/7)(1−1/11)(1−1/13)=23⋅45⋅67⋅1011⋅1213=576015015=3841001≈38.4%(1-1/3)(1-1/5)(1-1/7)(1-1/11)(1-1/13)=\frac23\cdot\frac45\cdot\frac67\cdot\frac{10}{11}\cdot\frac{12}{13}=\frac{5760}{15015}=\frac{384}{1001}\approx38.4\%.

So five tiny remainder checks — a few CPU cycles — discard over 61%61\% of composite odd candidates before the expensive modular-exponentiation test is ever called; real libraries extend this pre-sieve up to the first few hundred primes, throwing away ~80–90% of composites almost for free.

When sieving [2, 200] with the Sieve of Eratosthenes, what is the largest prime whose multiples need to be crossed off?

What is the value of the Möbius function μ(30)?

What is the "parity barrier" in sieve theory?

What fraction of random odd integers is not divisible by 3 or 5?

References

  1. Alina Carmen Cojocaru, M. Ram Murty (2005). An Introduction to Sieve Methods and Their Applications
  2. Yitang Zhang (2014). Bounded gaps between primes · DOI:10.4007/annals.2014.179.3.7
  3. James Maynard (2015). Small gaps between primes · DOI:10.4007/annals.2015.181.1.7