Sieve of Eratosthenes: Time Complexity using Inclusion-Exclusion Principle
Authored by: Rohan Thomas
Sieve of Eratosthenes is an ancient algorithm introduced by Greek mathematician Eratosthenes in the third century BC, used to find prime numbers up to a given number. In this material we will discuss this algorithm and determine the exact number of eliminations made.
Algorithm:
List all integers from 2 to nο»Ώ
Start with the smallest prime, set p=2ο»Ώ
Eliminate all the proper multiples of pο»Ώ which are less than nο»Ώ such as 2p,3p,4p,...ο»Ώ
Assign the value of pο»Ώ to the smallest number next greater than pο»Ώ which have been not eliminated, remove the multiples of that number
Repeat the steps until pβ€nβο»Ώ
Letβs consider the following example of listing all the primes less than n=15ο»Ώ
Generate a list of numbers from 2 to 15
2
3
4
5
6
7
8
9
10
11
12
13
14
15
Starting with the smallest prime p=2ο»Ώ, eliminate all multiples of 2
2
3
5
7
9
11
13
15
Assign the value of pο»Ώ to the next prime, which is 3, (since 3β€nβο»Ώ). Eliminate all the multiples of 3 to obtain.
2
3
5
7
11
13
4. When p=5ο»Ώ , the condition pβ€nβο»Ώ is false as 5>nβο»Ώ. Thus we exit the process, and now all the numbers in the list are prime numbers up to 15.
Time Complexity
The goal is to compute the number of eliminations took place for a given input n. For simplicity letβs just assume that every elimination has some cost associated with it (this includes computing the product, comparisons, etc.).
Letβs first see how many eliminations have been taken place for some input n. Recall we continue the procedure until p<nβο»Ώ, so, the primes involved in the procedure are P={2,3,5,β¦,pmβ,β¦,peβ}ο»Ώ
When p=2:ο»Ώ
p=3ο»Ώ: since we already removed all the even multiples, we have:
p=5ο»Ώ: there are βn/5βο»Ώ multiples of 5, from this we need to remove the multiples of 5 which are either divisible by 2 or 3
p=7:ο»Ώ
We can easily note the connections to inclusion-exclusionΒ principleο»Ώ. Let's generalize this to any arbitrary step involving pmβο»Ώ.
p=pmβο»Ώ
Consider the following sets, Am1ββο»Ώ= {x:xο»Ώ is a multiple of pmβο»Ώ and 2}, Am2ββο»Ώ={x:xο»Ώ is a multiple of pmβο»Ώ and 3}, Am4ββο»Ώ={x:xο»Ώ is a multiple of pmβο»Ώ and 5}, β―ο»Ώ , Amnββο»Ώ={x:xο»Ώ is a multiple of pmβο»Ώ and pmβ1βο»Ώ}.
notice there are βpmβnββο»Ώ multiples of pnβο»Ώ, but we need remove multiples of pmβο»Ώ which are divisible by either 2,3,5,β¦,ο»Ώpmβ1β.ο»Ώ This is exactly same as,
Thus, for a given nο»Ώ, we can write the time complexity as
where C is the cost per elimination
Here we obtained a more theoretical tighter run time than O(nloglogn)ο»Ώ counting the exact number of eliminations made. But in fact T(n)βO(nloglogn)ο»Ώ.