Time & Space Complexity of Sieve of Eratosthenes
In the previous article, we learned about what is sieve of Eratosthenes algorithm and how to implement it in java.
in this article, we are going to learn about the time and space complexity of sieve of the Eratosthenes algorithm.
Let us take an example when n = 50. So we need to print all prime numbers smaller than or equal to 50.
We create a table of all numbers from 2 to 50.
It involves methodically eliminating the numbers known not to be prime until only the prime numbers remain. Begin by crossing out 1 as it is not a prime number (it does not have two factors, it is a square number).
2 is a prime number but all of its multiples a not (they are composite numbers) so cross out all of the multiples of 2 but leave 2 as the first prime number.
Next, cross out all of the multiples of 3 except 3 itself.
The number 4 and all of its multiples have already been crossed out as they are also multiples of 2.
Next, cross out all the multiples of 5 except 5 itself.
Continue this process until you have discovered as many prime numbers as you need.
at first, we are eliminating all the numbers that are multiples of 2 and how many numbers multiple of 2 exist in the range till n ---> n/2
same goes for 3:--
we are eliminating all the numbers that are multiples of 3 and how many numbers multiple of 3 exist in the range till n ---> n/3
same for 5, 7 ......
the first time the loop is running for 2, now mark all the multiple of 2 as not prime. ok
same for 3, 5, 7 ......
So, Time Complexity:-
n/2 + n/3 + n/5 + n/7 + ..........
taking n common
n(1/2 + 1/3 + 1/5 + 1/7 + .........)
so the expression__
(1/2 + 1/3 + 1/5 + 1/7 + .........)
Proof of Harmonic Progression of the sum of primes: In order to understand the proof, the prerequisite is the harmonic serise and Taylor series expansion.
Lets take an the equation:
The taylor series expansion of the above equation is given by:
Putting x = 1 in the above equation, we get the relation:
Let’s mark the above equation as 1.
From Euler’s product formula,
On substituting s = 1 in the above equation, we get
On applying log to both the sides:
On simplifying the above equation, it becomes:
In the above equation, 1 > p-1 > -1
Thus, we can use taylor series expansion for the right hand side of the above equation.
On substituting this in the above equation, we get:
where p is a prime number.
On expanding the inner summation;
The above series is convergent. So, the above series can be approximated to:
Therefore, on substituting and rewriting the equation; we get
where p is the prime number.
From the initial equation 1, we can finally conclude that:
where p is the sum of prime numbers.
so now the total time complexity is O(n*log(log(n)))
Space Complexity:-
the space complexity of sieve of Eratosthenes is equal to the range of number.
means if we want to list all the prime numbers between 1 to n then time complexity is O(N)
Subscribe to my newsletter
Read articles from Nishant Jangid directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by
Nishant Jangid
Nishant Jangid
Posting daily on full-stack web & app development topics also covers data structure and algorithms. follow for the most insightful and easy-to-understand articles.