Created
July 17, 2023 17:28
-
-
Save patrickbucher/3227aebef0abc5358b0afeac7d14ff25 to your computer and use it in GitHub Desktop.
Sieve of Eratosthenes in Python
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
#!/usr/bin/env python3 | |
from functools import reduce | |
def sieve(xs): | |
return reduce( | |
lambda primes, x: primes + [x] | |
if not any(filter(lambda p: x % p == 0, primes)) | |
else primes, | |
xs, | |
[], | |
) | |
if __name__ == "__main__": | |
numbers = [i for i in range(2, 100)] | |
primes = sieve(numbers) | |
print(primes) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment