Sieve of Eratosthenes
Find every prime up to a limit by repeatedly marking multiples of the next unmarked number. A classic number-theory animation.
Each step picks the next unmarked number as prime and crosses out its multiples. Grey cells are composite; green cells are prime. When the animation stops, all primes up to 120 remain.
What you are seeing
Start at 2. The first unmarked number is prime.
Mark every multiple of that prime as composite.
Move to the next unmarked number and repeat until you reach the square root of the limit.
Why it matters
Prime sieves are used in cryptography, number theory, and competitive programming. The same idea appears in modern segmented and linear sieves.
Complexity & details
Time: O(n log log n). Space: O(n).
Frequently asked questions
Why stop at the square root?+
Every composite number up to n has a prime factor no larger than √n. Larger factors are already covered by smaller ones.
What is the difference from trial division?+
Trial division tests each number separately. The sieve marks many composites at once and is much faster for ranges.