1. Overview
In this tutorial, we’ll discuss the Sieve of Eratosthenes algorithm. We’ll also present the time complexity analysis of the algorithm.
2. Theoretic Idea
Given a number , we need to find all the prime numbers up to the input integer .
The Sieve of Eratosthenes algorithm is simple, but one of the most efficient ways to find them in a segment . The first step of the algorithm is to write down all the numbers from to the input number .
For any given input number greater than , the first prime would always be . So we’ll create a list of consecutive integers from to a specified input integer . At this point, we found the first prime number. Next, we’ll remove all the integers from our list, which are the multiples of (since is the first prime number).
We’ll continue choosing the next prime numbers and remove the multiples of the current prime number from our list until the current prime number is less than or equal to . Finally, all the integers which will remain in our list are our prime numbers.
3. Pseudocode
We discussed the steps of the Sieve of Eratosthenes algorithm, let’s see the pseudocode now:
algorithm SievePrime(N)
// INPUT
// N = a positive integer > 2
// OUTPUT
// All the prime numbers < N
L <- array of of size N filled with ones
result <- empty array
for k <- 2 to sqrt(N):
if L[k] = 1:
j = k * k
while j <= N:
L[j] <- 0
j <- j + k
for s <- 1 to N:
if L[s] = 1:
result.append(s)
return result
Here we take a number as an input and create an array of size . The index of the array represents the numbers, and the values inside the array denotes whether it’s a prime number or not. If the value inside is , its a composite number. Similarly, if the value inside is , it’s a prime number.
We know that is not a prime number, so we start the algorithm by initializing to and the rest of the indices to . In each iteration, we set a loop value and check whether it’s a composite or prime number. If it’s a prime number, we remove all the integers from our list which are the multiples of that prime number by assigning them .
We’ll run the algorithm until the current loop value is less than or equal to . Finally, we return all the indices whose value is .
4. Example
Let’s take . The first step is to create a list of size and assign the first index to and the rest with :
Now we’ll start the iteration. Initially, we set loop value to and check the index of the array if it contains the value or not. If the index in contains , we assign to all the indices, which are the multiples of current loop value:
Next, we’ll set the value of the loop to , and continue the same process:
The next loop value would be , but as we can see, its value is . So we’ll go to the next index that’s and repeat the same process:
Our next prime would be , but according to the algorithm, we’ll stop when the current prime number is less than or equal to . Here is , therefore is . So we’ll not continue with the next prime number and terminate the algorithm. The algorithm returns the indices which have value , and these are all prime numbers:
5. Time Complexity
The core part of this algorithm is to mark the composite numbers and remove them from the list by assigning . Now to mark a composite number and assign the value to it takes time. The real complexity of this algorithm lies in the number of times the loops run to mark the composite numbers.
In order to remove the composite numbers, we assign the value to the multiples of the current prime number in . Now let’s assume our current prime number is . In the first iteration, we’ll mark elements. Like this, when our current prime number is , we assign to composite numbers. The total number times we runs the loop would be equal to:
Let’s solve this equation:
Using Harmonic Progression of the sum of primes, it can be proved that:
Therefore, total time complexity of the Sieve of Eratosthenes algorithm would be .
6. Conclusion
In this tutorial, we’ve discussed the Sieve of Eratosthenes algorithm in detail. We presented the pseudocode of the algorithm and analyzed the time complexity.