Quantcast
Channel: using filter and generator to generator endless prime number in python - Stack Overflow
Viewing all articles
Browse latest Browse all 4

using filter and generator to generator endless prime number in python

$
0
0

Below is a python program I found to find prime numbers using Sieve of Eratosthenes. It uses filter and generator. I'm not able to understand it.

def _odd_iter():    n = 1    while True:        n = n + 2        yield ndef _not_divisible(n):    return lambda x: x % n > 0def primes():    yield 2    it = _odd_iter()    while True:        n = next(it)        yield n        it = filter(_not_divisible(n), it)for n in primes():    if n < 1000:        print(n)    else:        break

What I don't understand is it = filter(_not_divisible(n), it). For example for number 105, how is it excluded by this single line of code?


Viewing all articles
Browse latest Browse all 4

Latest Images

Trending Articles





Latest Images

<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>
<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596344.js" async> </script>