Nime

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.

n=abn = a * b

aa and bb are factors of nn.

n=nnn = \sqrt{n} * \sqrt{n}, so one factor has to be less than or equal to n\sqrt{n} while the other is greater than or equal to that square root.

anba \leq \sqrt{n} \leq b

Up to any number nn, all multiples of a number bigger than n\sqrt{n} must have a factor smaller than n\sqrt{n}. As a result that multiple will already be unmarked.

This means that all the non-primes limit\geq \sqrt{limit} will be unmarked in the process of checking every number limit\leq \sqrt{limit}.

Example

21=4.58\sqrt{21} = 4.58

Any number up to 2121 that is a multiple of a number larger than 4.584.58 will have a factor smaller than 4.584.58.

Because 1818 is a number up to 2121.
It is also a multiple of a number that is bigger than 4.584.58.

That means a factor of 1818 must be smaller than 4.584.58.

That checks out, 33 is a factor!

Because 33 is a factor of 1818. 1818 was unmarked while going through multiples when 33 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.

  • m=multiplem = multiple
  • k=multiplierk = multiplier
  • p=primep = prime
m=kpm = k * p

The number that is now pp, was previously kk for every smaller prime number.

Because kp=pkk * p = p * k, every multiple smaller than ppp * p has already been unmarked in a previous iteration.

Example

If our current detected prime, p=5p = 5.

55 was previously the multiplier for every smaller prime number.

  • 525 * 2 was unmarked when pp was 22, we don’t need to calculate 252 * 5
  • 535 * 3 was unmarked when pp was 33, we don’t need to calculate 353 * 5

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)

sieve.py
def primes_up_to(upper_bound):
# initialise sieve that marks all numbers as prime
sieve = [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.

sieve.py
def primes_up_to(upper_bound):
# initialise sieve that marks all numbers as prime
sieve = [True] * (upper_bound + 1)
# 0 and 1 are not prime
sieve[0] = False
sieve[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.

sieve.py
import math
def primes_up_to(upper_bound):
# initialise sieve that marks all numbers as prime
sieve = [True] * (upper_bound + 1)
# 0 and 1 are not prime
sieve[0] = False
sieve[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 prime
if 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.

sieve.py
import math
def primes_up_to(upper_bound):
# initialise sieve that marks all numbers as prime
sieve = [True] * (upper_bound + 1)
# 0 and 1 are not prime
sieve[0] = False
sieve[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 prime
if sieve[num]:
# unmark all multiples
# start unmarking at num squared
# every smaller multiple has already been unmarked in previous iterations
for 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.

sieve.py
import math
def primes_up_to(upper_bound):
# initialise sieve that marks all numbers as prime
sieve = [True] * (upper_bound + 1)
# 0 and 1 are not prime
sieve[0] = False
sieve[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 prime
if sieve[num]:
# unmark all multiples
# start unmarking at num squared
# every smaller multiple has already been unmarked in previous iterations
for multiple in range(num ** 2, upper_bound + 1, num):
sieve[multiple] = False
# sieve is done, turn `True` into numbers
return [idx for idx, mark in enumerate(sieve) if mark]

Final code

sieve.rs
pub fn primes_up_to(upper_bound: usize) -> Vec<usize> {
// initialise sieve that marks all numbers as prime
let mut sieve = vec![true; upper_bound + 1];
// 0 and 1 are not prime
sieve[0] = false;
sieve[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 2..=(upper_bound as f64).sqrt() as usize + 1 {
// if sieve[num] is true, then num is prime
if sieve[num] {
// unmark all multiples
// start unmarking at num squared
// every smaller multiple has already been unmarked in previous iterations
for multiple in (num * num..=upper_bound).step_by(num) {
sieve[multiple] = false;
}
}
}
// sieve is done, turn `true` into numbers
sieve
.iter()
.enumerate()
.filter_map(|(idx, mark)| match mark {
true => Some(idx),
false => None,
})
.collect()
}