Search⌘ K
AI Features

Sieve of Eratosthenes - Optimized

Explore how to optimize the Sieve of Eratosthenes algorithm using number theory concepts. Learn to skip even numbers and start marking multiples from the square of primes to reduce complexity to approximately linear time. This lesson helps you implement an efficient prime-checking method essential for coding interviews.

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} ...