python, 用filter实现素数
# _*_ coding:utf-8 _*_
#step1: 生成一个序列
def _odd_iter():
n = 1
while True:
n = n + 1
yield n
#Step2: 定义筛选函数
def _not_divisible(n):
return lambda x: x % n >0
#step3:定义生成器,不断的返回下一个素数
def primes():
it = _odd_iter()#初始序列
while True:
n = next(it)
yield n
it = filter(_not_divisible(n), it)
#test
for n in primes():
if n < 20:
print(n)
else:
break