NickyMeulemanNime

• ### By

• Nicky Meuleman

• ### Older post

One line copy button for the web

1. Method
2. Optimizations
1. Repeat until the square root
1. Example
2. Start unmarking at the square
1. Example
3. Step by step in code
4. Final code

# The Sieve of Eratosthenes

The sieve of Eratosthenes finds all prime numbers up to a given limit.

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)

sieve.py
def primes_up_to(upper_bound):  # initialise sieve that marks all numbers as prime.css-14ew794{background-color:#01121f;border-left-color:#9ccc65;border-left-style:solid;border-left-width:0.25rem;display:block;margin-right:-0.5rem;margin-left:-0.5rem;padding-right:0.5rem;padding-left:0.25rem;}  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).css-13aqjzy{display:inline-block;}
# 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
.css-1mjim83{display:inline-block;width:2ch;text-align:right;-webkit-user-select:none;-moz-user-select:none;-ms-user-select:none;user-select:none;opacity:0.3;margin-right:0.5rem;}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];4
5    // 0 and 1 are not prime6    sieve[0] = false;7    sieve[1] = false;8
9    // 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    }23
24    // 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);4
5  // 0 and 1 are not prime6  sieve[0] = false;7  sieve[1] = false;8
9  // 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  }23
24  // sieve is done, turn true into numbers25  const primes = [];26  for (const [idx, mark] of sieve.entries()) {27    mark && primes.push(idx);28  }29
30  return primes;31}
1import math2
3def primes_up_to(upper_bound):4  # initialise sieve that marks all numbers as prime5  sieve = [True] * (upper_bound + 1)6
7  # 0 and 1 are not prime8  sieve[0] = False9  sieve[1] = False10
11  # 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] = False22
23  # sieve is done, turn True into numbers24  return [idx for idx, mark in enumerate(sieve) if mark]

Designed and developed by Nicky Meuleman

Built with Gatsby. Hosted on Netlify.