用filter求素数

 1 def _odd_iter():
 2     n = 1
 3     while True:
 4         n = n+2
 5         yield n
 6         
 7 def _not_divisible(n)   :
 8     return lambda x: x % n >0
 9 
10 def primes():
11     yield 2
12     it = _odd_iter() #初始序列
13     while True:
14         n = next(it) #返回序列的第一个数
15         yield n
16         it = filter(_not_divisible(n), it)
17         
18         
19 for n in primes():
20     if n < 200:
21         print(n)
22     else:
23         break

 

posted @ 2018-03-24 17:41  WWBlog  阅读(154)  评论(0编辑  收藏  举报