Skip to content

Instantly share code, notes, and snippets.

@patrickbucher
Created July 17, 2023 17:28
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save patrickbucher/3227aebef0abc5358b0afeac7d14ff25 to your computer and use it in GitHub Desktop.
Save patrickbucher/3227aebef0abc5358b0afeac7d14ff25 to your computer and use it in GitHub Desktop.
Sieve of Eratosthenes in Python
#!/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