Finds all prime numbers up to a limit by eliminating multiples.
The Sieve of Eratosthenes is one of the oldest known algorithms, devised by the Greek mathematician Eratosthenes of Cyrene around 240 BC. Eratosthenes -- the same scholar who famously estimated the circumference of the Earth -- created this method for finding all prime numbers up to any given limit. The algorithm works by systematically eliminating the multiples of each discovered prime, leaving behind only the prime numbers themselves. Despite being over two thousand years old, it remains the standard and most efficient method for generating lists of small to moderate-sized primes, a testament to the elegance and durability of well-designed algorithms.
Eratosthenes served as the chief librarian at the Library of Alexandria and was one of the most versatile scholars of the ancient world. His sieve was described by Nicomachus of Gerasa in his "Introduction to Arithmetic" around 100 AD. The name "sieve" comes from the metaphor of sifting through numbers -- composite numbers fall through the holes while primes are retained. The algorithm has been continuously used for over two millennia, making it arguably the longest-lived algorithm still in practical use today.
The square root optimization is the key insight. For n = 100, you only need to sieve with primes up to 10 (that is, 2, 3, 5, and 7). After eliminating their multiples, every unmarked number remaining is guaranteed to be prime.
| Metric | Value |
|---|---|
| Time | O(n log log n) |
| Space | O(n) |
The time complexity of O(n log log n) arises from the harmonic series of reciprocals of primes: 1/2 + 1/3 + 1/5 + 1/7 + ... which grows as log log n. This makes the sieve remarkably efficient -- the log log n factor grows so slowly that log log(10^9) is only about 3. For all practical purposes, the sieve runs in nearly linear time relative to n.
The segmented sieve processes the range in blocks that fit in CPU cache, reducing memory usage from O(n) to O(square root of n) while maintaining the same time complexity. This variant can sieve ranges up to 10^12 or beyond. The sieve of Sundaram (1934) is an alternative that finds all odd primes using a different elimination pattern. The sieve of Atkin (2003) has a theoretically better asymptotic complexity of O(n / log log n) but is more complex to implement and only faster in practice for very large values of n.
The Sieve of Eratosthenes is the standard method for generating all primes up to a moderate limit, typically up to approximately 10^8 in a straightforward implementation. It is used extensively in number theory research, in cryptography for generating prime candidates for RSA and other public-key systems, in competitive programming where prime factorization or prime-counting problems are common, and in mathematical software libraries. For testing whether a single large number is prime, probabilistic tests like Miller-Rabin are preferred. The sieve also serves as a beautiful introduction to algorithmic thinking in education -- it demonstrates how a simple, systematic approach can solve a problem that seems inherently difficult.