Sieve of Eratosthenes Simulator: Watch Primes Emerge

simulator beginner ~8 min
Loading simulation...
46 primes — found below 200 using the Sieve of Eratosthenes

The Sieve of Eratosthenes finds 46 prime numbers up to 200 by iteratively eliminating multiples of each prime starting from 2.

Formula

π(N) ≈ N / ln(N) (Prime Number Theorem)
Sieve complexity: O(N log log N)
Largest sieve prime needed: p ≤ √N

The Oldest Algorithm

The Sieve of Eratosthenes, devised over two millennia ago, remains one of the most elegant algorithms in mathematics. The idea is breathtakingly simple: write down all numbers from 2 to N, then starting from 2, cross off every multiple. The first uncrossed number after each round is the next prime. Repeat until you have sieved past √N, and every surviving number is guaranteed prime. This simulation lets you watch the crossing-off process unfold in real time on a color-coded grid.

Why It Works

Every composite number has at least one prime factor no greater than its square root. By the time the sieve has processed all primes up to √N, every composite up to N has been marked at least once. This is why the sieve's outer loop only needs to run up to √N — a fact that makes the algorithm remarkably efficient at O(N log log N), near-linear in the size of the input range.

Patterns in the Grid

Watch the grid carefully and you will notice striking visual patterns. Multiples of 2 form every other cell — a checkerboard. Multiples of 3 create diagonal stripes depending on the column count. As higher primes are sieved, the pattern becomes increasingly irregular, reflecting the pseudo-random distribution of primes. Twin primes (pairs like 11,13 or 29,31) appear as adjacent survivors, growing rarer but never provably vanishing.

From Ancient Greece to Modern Cryptography

The primes discovered by this sieve are the foundation of RSA encryption, which secures internet commerce. While Eratosthenes used the sieve to study pure number theory, today's variants — segmented sieves, wheel sieves, and the Sieve of Atkin — generate primes at industrial scale. The fundamental insight remains unchanged: elimination by multiplication is the fastest path to discovering primes.

FAQ

What is the Sieve of Eratosthenes?

The Sieve of Eratosthenes is an ancient algorithm for finding all prime numbers up to a given limit. Starting from 2, it marks each multiple of every found prime as composite. Numbers that remain unmarked are prime. It was described by Eratosthenes of Cyrene around 240 BCE.

How efficient is the Sieve of Eratosthenes?

The algorithm runs in O(N log log N) time and O(N) space, making it one of the most efficient ways to generate all primes up to N. For N up to several million, it completes in milliseconds on modern hardware.

Why do you only sieve up to √N?

If a composite number n ≤ N has a prime factor, at least one factor must be ≤ √N. So after sieving all primes up to √N, every remaining unmarked number must be prime.

How many primes are there below a given number?

The Prime Number Theorem states that π(N) — the count of primes up to N — is approximately N / ln(N). More precise estimates use the logarithmic integral Li(N). For example, there are 25 primes below 100 and 168 below 1000.

Sources

Embed

<iframe src="https://homo-deus.com/lab/number-theory/prime-sieve/embed" width="100%" height="400" frameborder="0"></iframe>
View source on GitHub