title: Why doesn't the time complexity of Sieve of Eratosthenes algorithm have the argument sqrt(n)?
body: <p>I'm trying to understand the Sieve of Eratosthenes algorithm time complexity. Everywhere online, it says the time complexity is O(nloglog(n)), but I don't understand why.</p>
  
  <p>Here is some pseudocode</p>
  
  <pre><code>factors =  new int[n+1];
  for i from 2 to n
      factors[i] = 1; //true
  
  for i from 2 to sqrt(n)
      if(factors[i] == 1) //isPrime
      {
          for all multiples of i upto n
              factors[multiple] = 0 //notPrime
      } 
  
  return all indices of factors that have a value of 1
  </code></pre>
  
  <p>I think we can all agree that the time complexity of this function depends on the nested for loop. Now its analysis. When i = 2, the inner loop runs n/2 times. When i = 3, the inner loop runs n/3 times. The next time the inner loops executes is the next prime number so n/5 times. Altogether the loop will run</p>
  
  <p>n/2 + n/3 + n/5 + n/7 + ... times</p>
  
  <p>This is </p>
  
  <p>n(1/2 + 1/3 + 1/5 + 1/7 + ...)</p>
  
  <p>The sum of the reciprocals of primes up to n is a element of O(log(log(n))). 
  Thus, the overall complexity is O(nlog(log(n)))</p>
  
  <p>HOWEVER, as written in our pseudocode, the outer for loop only run root(n) times. Thus we are only summing the reciprocals of primes up to sqrt(n). So the complexity should beO(nlog(log(sqrt(n)))) not what is stated above.</p>
  
  <p>What is wrong with my analysis?</p>