Learn
Sieve of Eratosthenes
The Sieve of Eratosthenes is an ancient algorithm that finds all prime numbers up to by iteratively marking multiples of each prime as composite. It runs in time.
You start with (the first prime) and mark all its multiples. Then you move to the next unmarked number and repeat.
function sieve(n):
isPrime = array of size n + 1, initialized to true
isPrime[0] = false
isPrime[1] = false
for i from 2 to sqrt(n):
if isPrime[i]:
for j from i * i to n, step i:
isPrime[j] = false
return isPrime
Starting from : When marking multiples of , you start at because smaller multiples like , , etc. have already been marked by smaller primes.