Finding prime numbers is a nice way to see how a small change in approach can save a lot of work. The naive method asks, for every candidate, whether any smaller number divides it. The Sieve of Eratosthenes takes the opposite route: start with all candidates marked possible, then cross out the multiples of each prime.
The sieve
Here is a complete version in Python:
from math import isqrt
def primes_up_to(limit: int) -> list[int]:
if limit < 2:
return []
is_prime = [True] * (limit + 1)
is_prime[0] = is_prime[1] = False
for candidate in range(2, isqrt(limit) + 1):
if is_prime[candidate]:
# Smaller multiples were already crossed out.
for multiple in range(candidate * candidate, limit + 1, candidate):
is_prime[multiple] = False
return [number for number, prime in enumerate(is_prime) if prime]
print(primes_up_to(30))
# [2, 3, 5, 7, 11, 13, 17, 19, 23, 29]
Why start at the square?
The key detail is starting at candidate * candidate. Any smaller multiple has a smaller prime factor, so it was already removed earlier. We only need to try candidates up to : if a number has a factor larger than that, it must also have a factor smaller than that.
How it scales
For a limit , the sieve takes about time and space. That is a big improvement over checking every possible divisor for every number, while keeping the whole algorithm small enough to inspect line by line.
It is not a method for proving the Riemann Hypothesis. It is a useful computational tool for exploring primes—and a reminder that experiments can reveal patterns without proving an infinite claim.