Open In App

How is the time complexity of Sieve of Eratosthenes is n*log(log(n))?

Last Updated : 24 Jan, 2023
Like Article
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
  1. 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: 

\frac{n}{2} + \frac{n}{3} + \frac{n}{5} + \frac{n}{7} + ...... p

  1. On taking n common from the above equation, the above equation can be rewritten as:                                                                                                n \ast (1/2 + 1/3 + 1/5 + 1/7.....\infty )
  2. It can be proved as below with the help of Harmonic Progression of the sum of primes:                                                                                           \frac{1}{2} + \frac{1}{3} + \frac{1}{5} + \frac{1}{7} + ......  = log(log(n))   
  3. 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: log(\frac{x}{1 - x})
    • The taylor series expansion of the above equation is given by: x + \frac{x^2}{2} + \frac{x^3}{3} + ...
    • Putting x = 1 in the above equation, we get the relation: \sum_1^n \frac{1}{r} = log(n)   Let’s mark the above equation as 1.
    • From Euler’s product formula\sum_1^\infty \frac{1}{r^{s}} = \prod \frac{1}{1 - p^{-s}}
    • On substituting s = 1 in the above equation, we get \sum_1^\infty \frac{1}{r^{1}} = \prod \frac{1}{1 - p^{-1}}
    • On applying log to both the sides: log(\sum_1^\infty \frac{1}{r^{1}}) = log(\prod \frac{1}{1 - p^{-1}})
    • On simplifying the above equation, it becomes: log(\sum_1^\infty \frac{1}{r^{1}}) = \sum log( \frac{1}{1 - p^{-1}})
    • In the above equation, 1 > p-1 > -1
    • Thus, we can use taylor series expansion for the right hand side of the above equation. log( \frac{1}{1-x}) = \sum_1^\infty log( \frac{1}{n * p^{n}})
    • On substituting this in the above equation, we get: log( \sum_1^n \frac{1}{r}) = \sum \sum_1^n  \frac{1}{n * p^{n}}   where p is a prime number.
    • On expanding the inner summation; \sum_1^n \frac{1}{n * p^{n}} =  \frac{1}{p} + \frac{1}{2p^{2}} + \frac{1}{3p^{2}} + .....
    • The above series is convergent. So, the above series can be approximated to: \frac{1}{p}
    • Therefore, on substituting and rewriting the equation; we get log( \sum_1^\infty \frac{1}{n}) = \sum \frac{1}{p}   where p is the prime number.
    • From the initial equation 1, we can finally conclude that: \sum \frac{1}{p} = log(log(n))    where p is the sum of prime numbers. 
  4. On substituting this in the equation, we get the time complexity as: O(n*(log(log(n))))

Hence the time complexity of Sieve of Eratosthenes is n*log(log(n)) Reference: Divergence of the sum of the reciprocals of the primes

Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads