Guides

The Sieve of Eratosthenes: Finding Primes the Smart Way

The Sieve of Eratosthenes finds every prime up to a limit by repeatedly crossing out the multiples of each prime you find. Whatever survives is prime.

It's elegant, ancient and still one of the fastest ways to generate a list of primes.

How the sieve works

  1. Write the numbers 2 to N.
  2. Circle 2 (prime) and cross out every multiple of 2.
  3. Move to the next un-crossed number (3), circle it, cross out its multiples.
  4. Repeat. Everything still circled is prime.

A small example up to 30

The survivors are 2, 3, 5, 7, 11, 13, 17, 19, 23, 29 — the ten primes below 30. You only need to sieve multiples up to √N.

Why it's efficient

Each composite is crossed out by its prime factors rather than tested individually. This makes the sieve far faster than checking each number for primality when you want all primes in a range.

Sieve vs trial division

Use the sieve to generate many primes at once; use trial division (as in the Prime Number Checker) to test a single number.

Key takeaways
  • The sieve finds all primes up to N by crossing out multiples.
  • Only sieve up to the square root of N.
  • It's ideal for generating many primes at once.
  • For testing one number, trial division is simpler.

Prime Number Checker

Test individual numbers for primality.

Open the Prime Number Checker

Frequently asked questions

What is the Sieve of Eratosthenes?

An ancient algorithm that finds all prime numbers up to a limit by iteratively crossing out the multiples of each prime.

Why only cross out up to the square root?

Any composite number has a prime factor no larger than its square root, so larger primes have nothing new left to cross out.

Is the sieve faster than checking each number?

For generating all primes in a range, yes — it avoids testing every number individually.

The LCM Calculator Team

Math educators and engineers building free, accurate calculators with step-by-step solutions, visual diagrams and AI insights.