Virtual laboratory/Mathematics/Number theory/The Sieve of Eratosthenes

The Sieve of Eratosthenes

An extension of the topic of primes and divisibility. The Sieve of Eratosthenes (around 240 BC) is an algorithm for finding primes: we successively cross out the multiples of 2, 3, 5, 7…, and whatever remains are the primes. Euclid proved (around 300 BC) that there are infinitely many primes, even though they thin out among the successive numbers.

The Sieve of Eratosthenes
Mathematics · Number theory · The Sieve of Eratosthenes (~240 BC)
Range of numbers NN (up to which value we sieve)60
The further we count, the more rarely primes appear. The number of primes is…
range N
60
primes ≤ N
17
√N — sieving bound
7.7
density π(N)/N
28.3%
🎯 Challenge: prime or composite?
streak: 0

I'll give you a number — decide whether it is prime. Hint: it is enough to check divisors up to n\sqrt{n}.

What just happened?

The Sieve of Eratosthenes writes out the numbers 2, 3, 4, …, N, and then crosses out the multiples of each prime found — first 2 (4, 6, 8…), then 3 (6, 9, 12…), then 5, 7… Whatever is left uncrossed are the primes. A simple, mechanical recipe devised around 240 BC by Eratosthenes of Cyrene.

Why is it enough to sieve up to N\sqrt{N}?

Every composite number nn has a prime divisor no greater than n\sqrt{n}:

n=a⋅b,a≤b  ⇒  a≤n≤Nn = a\cdot b,\quad a\le b \;\Rightarrow\; a\le\sqrt{n}\le\sqrt{N}

If both factors were greater than n\sqrt{n}, their product would exceed nn — a contradiction. That is why every composite number is crossed out by some prime p≤Np\le\sqrt{N} and there is no need to sieve further. This is the key to the algorithm’s efficiency.

There are infinitely many primes (Euclid, ~300 BC):

In the “Elements” (Book IX) Euclid proves this elegantly by contradiction. Suppose there are finitely many primes: p1,p2,…,pkp_1, p_2, \ldots, p_k. Consider the number:

Q=p1⋅p2⋯pk+1Q = p_1\cdot p_2\cdots p_k + 1

The number QQ is not divisible by any of the pip_i (a remainder of 1 always stays). So either QQ is itself prime, or it has a prime divisor not on the list — in both cases there is a prime we did not include. A contradiction. The set of primes is therefore infinite: ∣{p:p prime}∣=∞|\{p : p \text{ prime}\}| = \infty.

They thin out, but never end

Among the successive natural numbers the primes appear more and more rarely — their density π(N)/N\pi(N)/N steadily decreases (visible on the bar to the right). Even so, they are never exhausted: you can always find the next one.

Why this matters for your exam:

The primes are the “building blocks” of all natural numbers — every natural number has a unique factorization into primes (the fundamental theorem of arithmetic). The Sieve of Eratosthenes and Euclid’s proof are classic school topics, and the primes are the foundation of modern cryptography (e.g. the RSA system bases its security on the difficulty of factoring large numbers into primes).

Remember: the Sieve of Eratosthenes crosses out the multiples of the primes up to N\sqrt{N} — because every composite number has a prime divisor ≤n\le\sqrt{n}. There are infinitely many primes (Euclid), although their density decreases.

Illustrative model · NexTutor Virtual Laboratory

Enjoyed the experiment? Share it.

It's free — send it to friends from school.

Theory & formulas

The algorithm

We write out 2,3,…,N2,3,\dots,N; we take the smallest number not yet crossed out (it is prime) and cross out its multiples; we repeat.

The sieving bound

It is enough to sieve up to N\sqrt{N} — every composite number n≤Nn\le N has a prime divisor ≤n≤N\le\sqrt{n}\le\sqrt{N}.

Infinitude of the primes

Euclid: if there were only finitely many (p1,…,pkp_1,\dots,p_k), then the number p1⋯pk+1p_1\cdots p_k+1 is not divisible by any of them — a contradiction. There are infinitely many primes.

Notation: NN the upper bound, N\sqrt{N} the sieving threshold, pip_i the primes.

Frequently asked questions

›

How many primes are there?

Infinitely many — Euclid proved this around 300 BC.

›

How does the Sieve of Eratosthenes work?

You successively cross out the multiples of the primes found so far; whatever is left uncrossed are the primes.

›

Why is it enough to sieve up to √N?

Because every composite number n has a prime divisor ≤ √n.

Teaching? Embed this experiment on your site.

Free widget — paste it on your school site, blog or notes. It links to the full version.

Teaching a class or tutoring? Guide students in NexTutor →