iyidgnaw
5/12/2018 - 1:42 AM

PrimeMaker.py

def _odd_iter():
    """
        generator for odd numbers
    """
    n = 1
    while True:
        n = n + 2
        yield n


def _not_divisible(n):
    """
        high order function
        return a function that judge the remainder of n
    """
    return lambda x: x % n > 0


def primes():
    """
        Very exicted program!!!
        Every time next() will go to the next line of "yield n"
        which will add another layer of filter to the already existing filters
        when n = 7 
        it = filter(_not_divisible(7),filter(_not_divisible(5),filter(_not_divisible(3),_odd_iter())))
        this is unbelivable simple solution...
    """
    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