常规方法:
def is_prime(n):
for i in range(2,n):
if n % i == 0:
return False
else:
return True
def numbers():
n = 2
while True:
yield n
n += 1
for i in filter(is_prime,numbers()):
if i < 100:
print(i,end=' ')
else:
break
埃式筛法:
def numbers():
n = 2
while True:
yield n
n += 1
def not_divisible(n):
return lambda x:x%n!=0
def primes():
it = numbers()
while True:
n = next(it) # 生成器numbers()的第一个数
yield n
it = filter(not_divisible(n), it)
for i in primes():
if i < 100:
print(i,end=',')
else:
break