使用过滤器和生成器在python中生成无限素数

时间:2017-01-16 03:03:19

标签: python algorithm generator yield

下面是我发现使用Sieve of Eratosthenes查找素数的python程序。它使用过滤器和发电机。我无法理解它。

def _odd_iter():
    n = 1
    while True:
        n = n + 2
        yield n

def _not_divisible(n):
    return lambda x: x % n > 0

def 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

我不明白的是it = filter(_not_divisible(n), it)。例如,对于数字105,如何通过这一行代码排除它?

3 个答案:

答案 0 :(得分:2)

这不仅仅是单个代码行,而是重复运行该行,具有不同的n值。

基本上,it是一个迭代器,它产生的候选素数尚未被筛子排除。你首先要做所有奇数候选人。

it = _odd_iter()

然后你反复拿走剩下的第一个候选人,

while True:
    n = next(it)

删除所有候选人的倍数,

    filter(_not_divisible(n), it)

并用删除倍数后留下的所有内容替换候选素数。

    it = ...

如果你假装filter返回一个数字列表,而不是一个可迭代的,并假装_odd_iter()返回一个奇数列表而不是一个可迭代的,你可以追踪循环并确定是什么在每个点的列表中。例如,运行后

it = _odd_iter()

你从

开始
it = 3, 5, 7, 9, 11, 13, 15, 17, 19, 21, 23, 25, ...

然后运行

    n = next(it) # 3

将第一个项目从前面拉开,留下你的

it = 5, 7, 9, 11, 13, 15, 17, 19, 21, 23, 25, ...

并运行

    it = filter(_not_divisible(3), it)

过滤掉3的所有倍数,

it = 5, 7, 11, 13, 17, 19, 23, 25, ...

然后返回到循环的顶部并从前面拉出新的第一个数字

    n = next(it) # 5

离开

it = 7, 11, 13, 17, 19, 23, 25, ...

然后过滤掉5的所有倍数,

    it = filter(_not_divisible(5), it)

给出了

it = 7, 11, 13, 17, 19, 23, ...

等等。

实际上,因为filter()返回迭代器而不是列表,所以最终会获得嵌套的迭代器序列。特别是,你从

开始
it = _odd_iter()

然后在循环的第一次迭代之后,基本上

it = filter(_non_divisible(3), _odd_iter())

除了从迭代器中取出3之外,然后在循环的第二次迭代之后

it = filter(_non_divisible(5), filter(_non_divisible(3), _odd_iter()))

除了5也来自迭代器,然后

it = filter(_non_divisible(7), filter(_non_divisible(5), filter(_non_divisible(3), _odd_iter())))

等等。

答案 1 :(得分:2)

对于找到的每个素数,filter应用于iterable,使用的过滤器是一个排除素数的所有倍数的函数。

所以你的iterable包含在你找到素数的过滤器中,例如数字105被排除,因为它可以被3整除,当你找到素数3时,所有3的倍数的过滤器被添加。

如果你添加一些print语句,它会更清楚一些(我希望):

def _odd_iter():
    n = 1
    while True:
        n = n + 2
        yield n

def _not_divisible(n):
    print('add filter for all multiples of', n)
    return lambda x: print('check if', x, 'is divisible by', n, 'result: ', not (x % n > 0)) or x % n > 0

def 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 < 20:
        print(n)
    else:
        break

打印:

2
3
add filter for all multiples of 3
check if 5 is divisible by 3 result:  False
5
add filter for all multiples of 5
check if 7 is divisible by 3 result:  False
check if 7 is divisible by 5 result:  False
7
add filter for all multiples of 7
check if 9 is divisible by 3 result:  True
check if 11 is divisible by 3 result:  False
check if 11 is divisible by 5 result:  False
check if 11 is divisible by 7 result:  False
11
add filter for all multiples of 11
check if 13 is divisible by 3 result:  False
check if 13 is divisible by 5 result:  False
check if 13 is divisible by 7 result:  False
check if 13 is divisible by 11 result:  False
13
add filter for all multiples of 13
check if 15 is divisible by 3 result:  True
check if 17 is divisible by 3 result:  False
check if 17 is divisible by 5 result:  False
check if 17 is divisible by 7 result:  False
check if 17 is divisible by 11 result:  False
check if 17 is divisible by 13 result:  False
17
add filter for all multiples of 17
check if 19 is divisible by 3 result:  False
check if 19 is divisible by 5 result:  False
check if 19 is divisible by 7 result:  False
check if 19 is divisible by 11 result:  False
check if 19 is divisible by 13 result:  False
check if 19 is divisible by 17 result:  False
19
add filter for all multiples of 19
check if 21 is divisible by 3 result:  True
check if 23 is divisible by 3 result:  False
check if 23 is divisible by 5 result:  False
check if 23 is divisible by 7 result:  False
check if 23 is divisible by 11 result:  False
check if 23 is divisible by 13 result:  False
check if 23 is divisible by 17 result:  False
check if 23 is divisible by 19 result:  False

答案 2 :(得分:1)

首先,过滤迭代器返回另一个迭代器。即当我们做类似的事情时:

it = filter(_not_divisible(3), it)
it = filter(_not_divisible(5), it)

我们得到一个链式迭代器“奇数并且不能被3整除而不能被5整除”。它有点类似于链式装饰器,我们得到相当于:

# assuming we have decorator @not divisible
@not_divisible(2)
def iter():
    return xrange(inf)

# then, at every subsequent prime we do something like:
iter = not_divisible(3)(iter)
# next prime is 5:
iter = not_divisible(5)(iter)

......等等