Section 10.2 Sieve of Eratosthenes
The Sieve of Eratosthenes is a method for finding all primes up to (and possibly including) a given natural \(n\text{.}\) This method works well when \(n\) is relatively small, allowing us to determine whether any natural number less than or equal to \(n\) is prime or composite.
We now explain how the Sieve of Eratosthenes can be used to find all prime numbers up to a given natural number. Recall that \(a\) is a multiple of \(b\) means that \(b\) divides \(a\text{,}\) see Definition 4.1.1.
Strategy 10.2.1. Sieve of Eratosthenes.
To find all prime numbers up to a given integer \(n\) we proceed as follows.
(a)
List all integers from \(2\) to \(n\)
(b)
The first integer on the list is \(2\text{,}\) and it is prime. Mark out all multiples of \(2\) that are bigger than \(2\) because they are composite.
We do not have to compute anything, as we can simply mark out every second number starting at \(2\text{.}\)
(c)
The next integer on the list that is not marked out is 3, and it is prime. Mark out all multiples of 3 that are bigger than 3 because they are composite. (Note that some of these, such as 6, will already be marked out).
We do not have to compute anything, as we can simply mark out every third number starting at \(3\text{.}\)
(d)
The next integer on the list that is not marked out is 5, and it is prime. Mark out all multiples of 5 that are bigger than 5 because they are composite.
We do not have to compute anything, as we can simply mark out every fifth number starting at \(5\text{.}\)
(e)
Continue in this way until there is no next integer on the list that is not marked out.
The integers that are not marked out are all of the primes up to (and possibly including) \(n\text{.}\)
The advantage of sieving is that any computing effort is replaced by counting.
In the video in Figure 10.2.1 we apply the sieve of Eratosthenes to the natural numbers up to \(100\text{.}\)
In Figure 10.2.2 we show the initial steps as well as the end result of the Sieve of Eratosthenes up to \(100\text{.}\) Namely:
All integers from \(2\) to \(100\) in Figure 10.2.(a)
All multiples of \(2\) marked out in Figure 10.2.(b)
All multiples of \(2\) and \(3\) marked out in Figure 10.2.(c)
All composites marked out in Figure 10.2.(d)
The unmarked numbers are all primes numbers up to \(100\text{,}\) Counting them we find that there are 25 primes up to \(100\text{.}\)
Explore the sieving process in Figure 10.2.3. Click on a number to have all its multiples marked by changing the field color to red and crossing them out. Numbers that you have clicked appear on green background. When there are no white fields left, the numbers in green fields are all the prime numbers up to 192.
It is most efficient to follow the strategy presented in Strategy 10.2.1. In particular that means first clicking on \(2\text{,}\) then clicking on the first number that remained white, which is \(3\) and so on. To illustrate the sieving better multiples are marked with a delay and different shades of red are used for multiples of different numbers.
The Sieve of Eratosthenes can be used to produce tables of primes such as in Table 10.2.4 without the need of factoring by simply counting.
In Checkpoint 10.2.5 count the number of prime numbers between two given numbers.
Checkpoint 10.2.5. Count Primes.
The number of primes from 0 to 10 is
The number of primes from 10 to 20 is
The number of primes from 20 to 30 is
The number of primes from 30 to 40 is
The number of primes from 40 to 50 is
The number of primes from 50 to 60 is
The number of primes from 60 to 70 is
The number of primes from 70 to 80 is
The number of primes from 80 to 90 is
The number of primes from 90 to 100 is
Checkpoint 10.2.5 illustrates that the number of prime numbers in sets of consecutive numbers of the same cardinality decreases as the numbers increase.