Complexity Analysis
In this lesson, we'll see the run-time analysis for Sieve.
The outer loop
for (int i = 2; i * i <= N; i++)
Runs for values but the number of iterations of the inner loop is dependent on .
for (int j = i + i; j <= N; j += i)
Also, note that the inner loop only runs for values when is prime.
Inner loop iterations | |
---|---|
2 | |
3 | |
4 | |
5 | |
6 | |
7 | |
8 |
Total number of operations:
The growth of the sum of the reciprocals of primes is . The proof is beyond the scope of this lesson but folks who would like to know more can go here.
This gives us the total time complexity of Sieve of Eratosthenes - .
In the next lesson, we’ll see a variation of Sieve for a higher range of integers.
Get hands-on with 1400+ tech skills courses.