Skip to content

Instantly share code, notes, and snippets.

@jorenham
Last active May 11, 2025 00:59
Show Gist options
  • Save jorenham/456edb0413539064a26d64f16d2c1ffe to your computer and use it in GitHub Desktop.
Save jorenham/456edb0413539064a26d64f16d2c1ffe to your computer and use it in GitHub Desktop.
Python walrus prime numbers
import itertools, typing
def primes(n: int, /) -> typing.Generator[int]:
yield next(sieve := itertools.count(prime := 2))
for _ in range(n):
yield (prime := next(sieve := filter(prime.__rmod__, sieve)))
@jorenham
Copy link
Author

jorenham commented Oct 8, 2024

>>> print(*primes(25))
2 3 5 7 11 13 17 19 23 29 31 37 41 43 47 53 59 61 67 71 73 79 83 89 97 101

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment