python, 用filter实现素数
生活随笔
收集整理的這篇文章主要介紹了
python, 用filter实现素数
小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
# _*_ coding:utf-8 _*_
#step1: 生成一個(gè)序列
def _odd_iter():
n = 1
while True:
n = n + 1
yield n
#Step2: 定義篩選函數(shù)
def _not_divisible(n):
return lambda x: x % n >0
#step3:定義生成器,不斷的返回下一個(gè)素?cái)?shù)
def primes():
it = _odd_iter()#初始序列
while True:
n = next(it)
yield n
it = filter(_not_divisible(n), it)
#test
for n in primes():
if n < 20:
print(n)
else:
break
轉(zhuǎn)載于:https://www.cnblogs.com/xiaohai2003ly/p/8119606.html
創(chuàng)作挑戰(zhàn)賽新人創(chuàng)作獎(jiǎng)勵(lì)來咯,堅(jiān)持創(chuàng)作打卡瓜分現(xiàn)金大獎(jiǎng)總結(jié)
以上是生活随笔為你收集整理的python, 用filter实现素数的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: webpack简单修改版本号(单页面)
- 下一篇: Python_python内置函数