Last active
July 16, 2024 16:55
-
-
Save ptmcg/a89be36f99ce0ecc5bbdc2fc027b6bbd to your computer and use it in GitHub Desktop.
Sieve of Eratosthenes - using filtering iterators
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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