find all multiplicands and multipliers for a number

Chris Angelico rosuav at gmail.com
Tue Apr 14 04:47:43 CEST 2015


On Tue, Apr 14, 2015 at 12:42 PM, Paul Rubin <no.email at nospam.invalid> wrote:
> Just for laughs, this prints the first 20 primes using Python 3's
> "yield from":
>
>     import itertools
>
>     def sieve(ps):
>         p = ps.__next__()
>         yield p
>         yield from sieve(a for a in ps if a % p != 0)
>
>     primes = sieve(itertools.count(2))
>     print(list(itertools.islice(primes,20)))

Small point: Calling dunder methods is usually a bad idea, so I'd
change this to "p = next(ps)" instead. But yep, that works...
inefficiently, but it works.

ChrisA



More information about the Python-list mailing list