首页 > 代码库 > 用生成器实现素数序列

用生成器实现素数序列

 1 def geneList():
 2     n = 1
 3     while True:
 4         n += 2
 5         yield n
 6 
 7 def myFilter(n):
 8     return lambda x : x % n > 0
 9 
10 def primeFilter():
11     yield 2
12     mGen = geneList()
13     while True:
14         n = next(mGen)
15         yield n
16         mGen = filter(myFilter(n), mGen)
17 
18 
19 for x in primeFilter():
20     if x <1000:
21         print(x)
22     else:
23         break

 

用生成器实现素数序列