Skip to main content

Sieve of Eratosthenes

Definition​

The Sieve of Eratosthenes is an efficient algorithm for finding all prime numbers up to a specified integer. It works by iteratively marking the multiples of each prime number starting from 2, which are not prime, until no more multiples can be found

Practice​

sieve_of_eratosthenes(maxValue):
Let isPrime be an array of Boolean values, indexed from 0 to maxValue, initialized to true
isPrime[0] = false
isPrime[1] = false

for i from 2 to square_root(maxValue):
if isPrime[i] is true:
for j from i^2 to maxValue with step i:
isPrime[j] = false

primes = empty list
for i from 2 to maxValue:
if isPrime[i] is true:
append i to primes

return primes