The Sieve of Eratosthenes
The sieve of Eratosthenes finds all prime numbers up to a given limit.
Method
The algorithm starts out by assuming all numbers are prime, and marking them as such. At the end of the algorithm, only prime numbers up to an upper limit will still be marked.
The number 1 is a special case, so we start off by unmarking it.
Then we go through the numbers one by one. For every non-prime number we find, skip to the next number.
If a number is still marked as prime when we get to it, that means it is prime.
Before moving on to the next number, we first unmark every multiple of the found prime.
Those multiples can be divided through the prime number we just found, so by definition isn’t prime.
We repeat this process until we reach the upper limit.
Every number that is still marked as prime, is truly prime.
Optimizations
By using some math we can do significantly less work while still getting the same result.
Repeat until the square root
While iterating through all numbers, we can stop at the square root of the upper limit.
Any non-prime can be expressed as the product of 2 numbers that are not 1 or itself.
and are factors of .
, so one factor has to be less than or equal to while the other is greater than or equal to that square root.
Up to any number , all multiples of a number bigger than must have a factor smaller than . As a result that multiple will already be unmarked.
This means that all the non-primes will be unmarked in the process of checking every number .
Example
Any number up to that is a multiple of a number larger than will have a factor smaller than .
Because is a number up to .
It is also a multiple of a number that is bigger than .
That means a factor of must be smaller than .
That checks out, is a factor!
Because is a factor of . was unmarked while going through multiples when was the number the algorithm was unmarking multiples for!
Start unmarking at the square
During the step the algorithm unmarks all multiples of a number. We can start unmarking at that number squared.
Every smaller multiple was already unmarked in a previous iteration.
Why?
A multiple can be written as a multiplier times a number.
The number that is now , was previously for every smaller prime number.
Because , every multiple smaller than has already been unmarked in a previous iteration.
Example
If our current detected prime, .
was previously the multiplier for every smaller prime number.
- was unmarked when was , we don’t need to calculate
- was unmarked when was , we don’t need to calculate
Step by step in code
The goal is to write a function that returns a list of prime numbers, up to upper_bound
.
We initialise a list of booleans that is 1 bigger than the given upper_bound
and call it sieve
.
These booleans tell us if the number at that index is prime or not. (True
for prime, False
for not)
def primes_up_to(upper_bound):# initialise sieve that marks all numbers as primesieve = [True] * (upper_bound + 1)
Smart people decided programmers start counting at 0, so that’s why that list is 1 bigger than upper_bound
.
It’s also the reason why we have to unmark the index 0 along with the index 1 before we start our loop.
def primes_up_to(upper_bound):# initialise sieve that marks all numbers as primesieve = [True] * (upper_bound + 1)# 0 and 1 are not primesieve[0] = Falsesieve[1] = False
This works out perfectly, because now every index exactly matches the number it represents.
You want to know if the number 69 is prime? The boolean at index 69 will tell you. Nice!
Loop over every number, starting at 2 and ending at the square root of upper_bound
.
Inside the loop, index sieve
with that number.
import mathdef primes_up_to(upper_bound):# initialise sieve that marks all numbers as primesieve = [True] * (upper_bound + 1)# 0 and 1 are not primesieve[0] = Falsesieve[1] = False# iterate up to square root of upper_bound# reason: if one factor of num is bigger than sqrt(upper_bound),# an other factor _must_ be smaller than sqrt(upper_bound)for num in range(2, math.floor(math.sqrt(upper_bound)) + 1):# if sieve[num] is true, then num is primeif sieve[num]:
If the boolean at that location is True
, the number is prime and we unmark every multiple before moving on to the next step of our loop.
Do this by skip counting.
Start at the number squared and add the number until you hit upper_bound
.
For every encountered multiple, set sieve
at that number’s index to False
.
import mathdef primes_up_to(upper_bound):# initialise sieve that marks all numbers as primesieve = [True] * (upper_bound + 1)# 0 and 1 are not primesieve[0] = Falsesieve[1] = False# iterate up to square root of upper_bound# reason: if one factor of num is bigger than sqrt(upper_bound),# an other factor _must_ be smaller than sqrt(upper_bound)for num in range(2, math.floor(math.sqrt(upper_bound)) + 1):# if sieve[num] is true, then num is primeif sieve[num]:# unmark all multiples# start unmarking at num squared# every smaller multiple has already been unmarked in previous iterationsfor multiple in range(num ** 2, upper_bound + 1, num):sieve[multiple] = False
At the end of the outer loop, sieve
will be full of booleans corresponding to the primeness of every possible index to that list.
Use your favourite method to loop over a list while also getting the index, put the indexes with a true
into a new list, and presto, primes.
import mathdef primes_up_to(upper_bound):# initialise sieve that marks all numbers as primesieve = [True] * (upper_bound + 1)# 0 and 1 are not primesieve[0] = Falsesieve[1] = False# iterate up to square root of upper_bound# reason: if one factor of num is bigger than sqrt(upper_bound),# an other factor _must_ be smaller than sqrt(upper_bound)for num in range(2, math.floor(math.sqrt(upper_bound)) + 1):# if sieve[num] is true, then num is primeif sieve[num]:# unmark all multiples# start unmarking at num squared# every smaller multiple has already been unmarked in previous iterationsfor multiple in range(num ** 2, upper_bound + 1, num):sieve[multiple] = False# sieve is done, turn `True` into numbersreturn [idx for idx, mark in enumerate(sieve) if mark]
Final code
1pub fn primes_up_to(upper_bound: usize) -> Vec<usize> {2 // initialise sieve that marks all numbers as prime3 let mut sieve = vec![true; upper_bound + 1];45 // 0 and 1 are not prime6 sieve[0] = false;7 sieve[1] = false;89 // iterate up to square root of upper_bound10 // reason: if one factor of num is bigger than sqrt(upper_bound),11 // an other factor _must_ be smaller than sqrt(upper_bound)12 for num in 2..=(upper_bound as f64).sqrt() as usize + 1 {13 // if sieve[num] is true, then num is prime14 if sieve[num] {15 // unmark all multiples16 // start unmarking at num squared17 // every smaller multiple has already been unmarked in previous iterations18 for multiple in (num * num..=upper_bound).step_by(num) {19 sieve[multiple] = false;20 }21 }22 }2324 // sieve is done, turn `true` into numbers25 sieve26 .iter()27 .enumerate()28 .filter_map(|(idx, mark)| match mark {29 true => Some(idx),30 false => None,31 })32 .collect()33}
1function primesUpTo(upperBound) {2 // initialise sieve that marks all numbers as prime3 const sieve = Array.from({ length: upperBound + 1 }, () => true);45 // 0 and 1 are not prime6 sieve[0] = false;7 sieve[1] = false;89 // iterate up to square root of upperBound10 // reason: if one factor of num is bigger than sqrt(upperBound),11 // an other factor _must_ be smaller than sqrt(upperBound)12 for (let num = 2; num <= Math.sqrt(upperBound) + 1; num++) {13 // if sieve[num] is true, then num is prime14 if (sieve[num]) {15 // unmark all multiples16 // start unmarking at num squared17 // every smaller multiple has already been unmarked in previous iterations18 for (let multiple = num ** 2; multiple <= upperBound; multiple += num) {19 sieve[multiple] = false;20 }21 }22 }2324 // sieve is done, turn `true` into numbers25 const primes = [];26 for (const [idx, mark] of sieve.entries()) {27 mark && primes.push(idx);28 }2930 return primes;31}
1import math23def primes_up_to(upper_bound):4 # initialise sieve that marks all numbers as prime5 sieve = [True] * (upper_bound + 1)67 # 0 and 1 are not prime8 sieve[0] = False9 sieve[1] = False1011 # iterate up to square root of upper_bound12 # reason: if one factor of num is bigger than sqrt(upper_bound),13 # an other factor _must_ be smaller than sqrt(upper_bound)14 for num in range(2,math.floor(math.sqrt(upper_bound)) + 1):15 # if sieve[num] is true, then num is prime16 if sieve[num]:17 # unmark all multiples18 # start unmarking at num squared19 # every smaller multiple has already been unmarked in previous iterations20 for multiple in range(num**2, upper_bound + 1, num):21 sieve[multiple] = False2223 # sieve is done, turn `True` into numbers24 return [idx for idx, mark in enumerate(sieve) if mark]