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

python实现斐波那契数列

2018-04-11 16:57 375 查看
'''
斐波那契数列指的是这样一个数列 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233,377,610,987,1597,2584,4181,6765,10946,17711,28657,46368........
这个数列从第3项开始,每一项都等于前两项之和。
'''

month1 = int(input('输入月数:'))
month2 = month1
# 第一种方法
a , b = 0 , 1
while month1 != 0:
print(b, end=',')
a , b = b , a+b
month1 -= 1

# 第二种方法
def fbnqQueue(month_temp):
num_list = []
for i in range(month_temp):
if i == 0 or i == 1:
num_list.append(1)
else:
num_list.append(num_list[i-1] + num_list[i-2])
return num_list

fbnq_queue = fbnqQueue(month2)
print()
print(fbnq_queue)
运行结果:输入月数:10
1,1,2,3,5,8,13,21,34,55,
[1, 1, 2, 3, 5, 8, 13, 21, 34, 55]
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: