您的位置:首页 > 编程语言 > Python开发

素数生成器

2016-06-04 15:36 337 查看
#!/usr/bin/python3
#coding=utf-8

__author__ = 'xdlove'

'素数生成器'

#奇数生成器(不包括1)生成的是一个无限序列

def odd_list():
n = 1
while True:
n += 2
yield n

#过滤因子---

def filter_not_prime(n):
return lambda x : x % n #返回闭包

#生成素数序列

def primes():
yield 2
it = odd_list()
while True:
n = next(it)
yield n
it = filter(filter_not_prime(n),it)

#遍历素数生成器

for i,x in enumerate(primes()): # enumerate迭代遍历键值对(索引:素数)
if i > 5:
break
else:
print(x)
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  python trade