How is the time complexity of Sieve of Eratosthenes is n*log(log(n))? Last Updated : 12 Jul, 2025 Comments Improve Suggest changes 67 Likes Like Report 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 EratosthenesIf 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 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 )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)) 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 > -1Thus, 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. 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 Create Quiz Comment K KaashyapMSK Follow 67 Improve K KaashyapMSK Follow 67 Improve Article Tags : DSA time complexity sieve Prime Number Explore DSA FundamentalsLogic Building Problems 2 min read Analysis of Algorithms 1 min read Data StructuresArray Data Structure 3 min read String in Data Structure 2 min read Hashing in Data Structure 2 min read Linked List Data Structure 2 min read Stack Data Structure 2 min read Queue Data Structure 2 min read Tree Data Structure 2 min read Graph Data Structure 3 min read Trie Data Structure 15+ min read AlgorithmsSearching Algorithms 2 min read Sorting Algorithms 3 min read Introduction to Recursion 15 min read Greedy Algorithms 3 min read Graph Algorithms 3 min read Dynamic Programming or DP 3 min read Bitwise Algorithms 4 min read AdvancedSegment Tree 2 min read Binary Indexed Tree or Fenwick Tree 15 min read Square Root (Sqrt) Decomposition Algorithm 15+ min read Binary Lifting 15+ min read Geometry 2 min read Interview PreparationInterview Corner 3 min read GfG160 3 min read Practice ProblemGeeksforGeeks Practice - Leading Online Coding Platform 1 min read Problem of The Day - Develop the Habit of Coding 5 min read Like