美文网首页
斐波那契生成器版本

斐波那契生成器版本

作者: 西凉0 | 来源:发表于2020-03-08 22:07 被阅读0次
# 0 1 1 2 3 5 8 13 21……生成器斐波那契

def fb():
    x=0
    y=1
    while True:
        n=x+y
        x=y
        y=n
        yield n

fb=fb()
print(0)
for i in range(20):
    print(next(fb))

# 0 1 1 2 3 5 8 13 21……生成器斐波那契

def fb():
    x,y = 0,1
    while True:
        yield x+y
        x,y = y,x+y

fb=fb()
print(0)
for i in range(20):
    print(next(fb))

相关文章

网友评论

      本文标题:斐波那契生成器版本

      本文链接:https://www.haomeiwen.com/subject/jthsdhtx.html