首页 > 技术文章 > Python 斐波那契数列

anthinia 2020-10-19 15:52 原文

斐波那契数列指的是这样一个数列 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)

 

推荐阅读