Return to Notes
Sieve of Eratosthenes
Determines all primes in a bounded range.
The Algorithm
Let . For each , if , remove all multiples in the list. The numbers left in are all the primes less than the bound .
Sage Implementation
bound = 1000
space = [2..bound]
sieve = lambda k: k % i != 0 or k == i
for i in [2..floor(sqrt(bound))]:
if i in space:
space = list(filter(sieve, space))
# We can check our work:
# primes = list(filter(is_prime, [2..bound]))
# print(len(primes), len(space))
# Both lists contain 168 values!
print(space)