Sieve of Eratosthenes - Optimized

Build an optimized solution of implementing the Sieve of Eratosthenes.

Can we still do better than our previous code?

Definitely! Here, we don’t need to check for even numbers. Instead of starting the second loop from 2p, we can start from p2{p^2}. Have a look at the code below.

Level up your interview prep. Join Educative to access 80+ hands-on prep courses.