Skip to content

Instantly share code, notes, and snippets.

@ptmcg
Last active July 16, 2024 16:55
Show Gist options
  • Save ptmcg/a89be36f99ce0ecc5bbdc2fc027b6bbd to your computer and use it in GitHub Desktop.
Save ptmcg/a89be36f99ce0ecc5bbdc2fc027b6bbd to your computer and use it in GitHub Desktop.
Sieve of Eratosthenes - using filtering iterators
import functools
import itertools
from collections.abc import Iterator
def is_multiple_of(p: int, x: int) -> bool:
# if x % p == 0:
# print("discarding", x, "multiple of", p)
return x % p == 0
def sieve_of_eratosthenes(n: int) -> Iterator[int]:
# start with list of all integers, starting with 2
ints: Iterator[int] = itertools.count(start=2)
for _ in range(n):
next_prime = next(ints)
yield next_prime
# wrap ints iterator in another filter, removing
# multiples of next_prime
ints = itertools.filterfalse(
functools.partial(is_multiple_of, next_prime),
ints,
)
# print the first 30 prime numbers
print(list(sieve_of_eratosthenes(30)))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment