Python 斐波那契数列

斐波那契数列指的是这样一个数列 0, 1, 1, 2, 3, 5, 8, 13,特别指出:第0项是0,第1项是第一个1。从第三项开始,每一项都等于前两项之和。

Python 实现斐波那契数列代码如下:

# 生成一个包含24 个斐波那契数列的列表
n1 = 0
n2 = 1
list = [0,1]
count = 2
nterms = 24  #你需要生成多少个
while count <= nterms:
    nth = n1 + n2
    list.append(nth)
    #更新值
    n1, n2 = n2, nth
    count += 1
print(list)

本文来自博客园,作者:anthinia,转载请注明原文链接:https://www.cnblogs.com/anthinia/p/13840645.html

原文地址:https://www.cnblogs.com/anthinia/p/13840645.html