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..60: slide n to cross out multiples of primes ≤n (red); the surviving green cells are primes.
SchoolThe Sieve of Eratosthenes
Definition: Sieve of Eratosthenes
To list every prime up to N: write down 2,3,…,N; repeatedly take the smallest unmarked number p, declare it prime, and mark every multiple of p (starting from p2) as composite; stop once p2>N.
P(z)=p<z∏p
Why stop at p2>N? Any composite n≤N has a prime factor ≤N (otherwise its two smallest prime factors would multiply to more than N), so once every prime up to N has done its marking, everything left unmarked genuinely has no factor to expose it — it must be prime. The product P(z)=∏p<zp, over all primes p<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 applied
Newly marked composite
Remaining 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≥2 is prime if and only if it is not divisible by any prime p≤n.
Why is it true?
This equivalence is exactly what lets the sieve stop early: once every prime up to 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
(⇐) Suppose n has no prime factor ≤n. If n were composite, write n=ab with 1<a≤b<n. Then a≤n (otherwise a>n and b≥a>n would force ab>n, a contradiction), and a's smallest prime factor p satisfies p≤a≤n, so p is a prime factor of n that is ≤n — contradicting the hypothesis. So n has no such factorization, hence n is prime.
(⇒) If n is prime, its only positive divisors are 1 and n; no prime p<n divides it at all, so in particular none with p≤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, everything still unmarked in [2,N] satisfies the right-hand side, hence is prime.
For A={1,2,…,x} and P the set of primes p<z, the count of a∈A coprime to P(z)=∏p<zp is S(A,P,z)=d∣P(z)∑μ(d)⌊dx⌋, where μ is the Möbius function; taking z=x+1 gives π(x)−π(x)+1=S(A,P,x)=∑d∣P(x)μ(d)⌊x/d⌋.
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∈A is coprime to P(z) iff it is divisible by none of the primes p<z. For each divisor d∣P(z) (a squarefree product of some subset of these primes), the count of multiples of d in A={1,…,x} is exactly ⌊x/d⌋.
By inclusion–exclusion over the events "p∣a" for p<z: the count of a∈A divisible by at least one prime in P is ∑p<z⌊x/p⌋−∑p<q⌊x/(pq)⌋+∑p<q<r⌊x/(pqr)⌋−⋯, alternating with the number of primes multiplied together. Each such alternating sign is exactly the Möbius function μ(d) of the corresponding squarefree d∣P(z): μ(1)=1, μ(d)=(−1)k for d a product of k distinct primes.
So the count divisible by at least one prime in P is −∑d∣P(z),d>1μ(d)⌊x/d⌋. Subtracting this from ∣A∣=⌊x⌋=x (the d=1 term, μ(1)⌊x/1⌋=x) gives the count coprime to P(z): S(A,P,z)=x−∑d∣P(z),d>1μ(d)⌊x/d⌋=∑d∣P(z)μ(d)⌊x/d⌋.
Finally, taking z=x+1 so that P is exactly the primes ≤x: any a∈[2,x] coprime to all of them is either 1 or a prime >x (by the theorem above, since it has no prime factor ≤x), so S(A,P,x+1)=1+(π(x)−π(x)), giving the stated identity.
π(x)−π(x)+1=S(A,P,x)=d∣P(x)∑μ(d)⌊x/d⌋
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=30 and z=6 (so the sieving primes are 2,3,5, since 30≈5.48) to compute π(30) from scratch.
Solution
Here P(6)=2⋅3⋅5=30, whose 23=8 squarefree divisors are 1,2,3,5,6,10,15,30.
Evaluate μ(d)⌊30/d⌋ for each: +⌊30/1⌋=30; −⌊30/2⌋−⌊30/3⌋−⌊30/5⌋=−15−10−6=−31; +⌊30/6⌋+⌊30/10⌋+⌊30/15⌋=+5+3+2=+10; −⌊30/30⌋=−1.
Summing gives S(A,P,6)=30−31+10−1=8. By Legendre's identity π(30)−π(30)+1=8, and π(30)=π(5)=3 (the primes 2,3,5), so π(30)=8+3−1=10 — matching the 10 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,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,13 (and 2, already fixed to odd) are independent over a full period 2⋅3⋅5⋅7⋅11⋅13=30,030.
A random odd integer is not divisible by p with probability 1−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)=32⋅54⋅76⋅1110⋅1312=150155760=1001384≈38.4%.
So five tiny remainder checks — a few CPU cycles — discard over 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?