def _odd_iter(): n = 1 while True: n = n + 2 yield n #2把偶数都筛选了 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) # 构造新序列 def sieve(m): s=[] for n in primes(): if n < m: s.append(n) else: break return sprint(sieve(100))
输出:
[2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97]