首页 > 编程知识 正文

斐波那契数列用python怎么写,斐波那契数列python编程

时间:2023-05-03 15:02:31 阅读:203520 作者:430

下面是自己写着玩的:
a=1
b=1
temp=int(input(“输入第几个数:”))
if temp == 1:
print(“第”,temp,“个数是”,0)
elif temp2 or temp3:
print(“第”,temp,“个数是”,1)
else:
for i in range(temp-3):
sum=a+b
a=b
b=sum
print(“输出第”,temp,“个数是:”,b)

总结这里的核心思想是怎么,怎么把后面一个数赋值给前面的一个数,用到一个介质(sum作为桥梁),不断赋值传递。编程思想,就是抽象的过程,抽象表达需要介质。

================================================================================菜鸟网站上的例子

-- coding: UTF-8 -- Filename : test.py author by : www.runoob.com Python 斐波那契数列实现 获取用户输入数据

nterms = int(input(“你需要几项?”))

第一和第二项

n1 = 0
n2 = 1
count = 2

判断输入的值是否合法

if nterms <= 0:
print(“请输入一个正整数。”)
elif nterms == 1:
print(“斐波那契数列:”)
print(n1)
else:
print(“斐波那契数列:”)
print(n1,",",n2,end=" , “)
while count < nterms:
nth = n1 + n2
print(nth,end=” , ")
# 更新值
n1 = n2
n2 = nth
count += 1

执行以上代码输出结果为:

你需要几项? 10
斐波那契数列:
0 , 1 , 1 , 2 , 3 , 5 , 8 , 13 , 21 , 34 ,

下面是网友的总结:
https://www.cnblogs.com/areyouready/p/8979973.html

https://www.cnblogs.com/panlq/p/9307203.html

https://www.runoob.com/python3/python3-fibonacci-sequence.html

版权声明:该文观点仅代表作者本人。处理文章:请发送邮件至 三1五14八八95#扣扣.com 举报,一经查实,本站将立刻删除。