Pre-requisite: Sieve of Eratosthenes
What is Sieve of Eratosthenes algorithm?
In order to analyze it, let’s take a number n and the task is to print the prime numbers less than n. Therefore, by definition of Sieve of Eratosthenes, for every prime number, it has to check the multiples of the prime and mark it as composite. This process continues until a value p which is the highest prime number less than n.
Understanding the n*log(log n) time complexity of Sieve of Eratosthenes
- If it is assumed that the time taken to mark a number as composite is constant, then the number of times the loop runs is equal to:
- On taking n common from the above equation, the above equation can be rewritten as:
- It can be proved as below with the help of Harmonic Progression of the sum of primes:
Proof of Harmonic Progression of the sum of primes:
In order to understand the proof, the prerequisite is the Harmonic series 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.
- Lets take an the equation:
- On substituting this in the equation, we get the time complexity as:
Hence the time complexity of Sieve of Eratosthenes is n*log(log(n))
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
- Sieve of Eratosthenes in 0(n) time complexity
- Sieve of Eratosthenes
- Sum of all Primes in a given range using Sieve of Eratosthenes
- Queries of nCr%p in O(1) time complexity
- Time Complexity of Loop with Powers
- Extended Mo's Algorithm with ≈ O(1) time complexity
- Check for balanced parentheses in an expression | O(1) space | O(N^2) time complexity
- Segmented Sieve
- Sieve of Atkin
- Bitwise Sieve
- Prime Factorization using Sieve O(log n) for multiple queries
- Sieve of Sundaram to print all primes smaller than n
- Number of unmarked integers in a special sieve
- Segmented Sieve (Print Primes in a Range)
- Longest sub-array of Prime Numbers using Segmented Sieve
- Sum of first n odd numbers in O(1) Complexity
- Cyclomatic Complexity
- Complexity Analysis of Binary Search
- Complexity analysis of various operations of Binary Min Heap
- Minimum time to reach a point with +t and -t moves at time t
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.