Skip to content

Instantly share code, notes, and snippets.

@brainix
Last active September 25, 2020 09:19
Show Gist options
  • Save brainix/8d1cfc1106f1db828a7bdcbdb3a23c78 to your computer and use it in GitHub Desktop.
Save brainix/8d1cfc1106f1db828a7bdcbdb3a23c78 to your computer and use it in GitHub Desktop.
>>> def naturals(start):
... yield start
... yield from naturals(start+1)
...
>>> def sieve(prev_sieve):
... prime = next(prev_sieve)
... yield prime
... yield from (num for num in prev_sieve if num % prime != 0)
...
>>> s = sieve(naturals(2))
>>> next(s)
2
>>> next(s)
3
>>> next(s)
5
>>> next(s)
7
>>> next(s)
9
>>> next(s)
11
>>> next(s)
13
>>> next(s)
15
>>> next(s)
17
>>> next(s)
19
>>> next(s)
21
>>> next(s)
23
>>> next(s)
25
>>> next(s)
27
>>> next(s)
29
>>> next(s)
31
>>> next(s)
33
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment