python斐波那契数列计算_python计算斐波那契数列

斐波那契数列就是黄金分割数列

第一项加第二项等于第三项,以此类推

第二项加第三项等于第四项

代码如下

这一段代码实现fib(n)函数返回第n项,PrintFN(m,n,i)函数实现输出第i项斐波那契数列,输出在m到n之间的斐波那契数的数量

def fib(n) :

x = 0

x1 = 1

x2 = 1

i = 2

while i <= n :

i = i + 1

x =x1 + x2

x1 = x2

x2 = x

if (n == 1 or n == 2) :

x = 1

return x

def PrintFN(m,n,i):

c = i

index = 0

while fib(i) < 10000 :

i = i + 1

count = i

i = 1

while i < count :

i = i + 1

if fib(i) >= m and fib(i) <= n :

index = index + 1

v = fib(c)

v = str(v)

c = str(c)

print("fib(" + c + ")" + "=" + v)

print(index)

m,n,i=input().split()

n=int(n)

m=int(m)

i=int(i)

PrintFN(m,n,i)

另一段代码如下

这一段代码实现fib(n)函数返回第n项,PrintFN(m,n,i)函数实现输出在m到n之间的所有的斐波那契数

如果没有,显示No Fibonacci number

def fib(n) :

x = 0

x1 = 1

x2 = 1

i = 2

while i <= n :

i = i + 1

x =x1 + x2

x1 = x2

x2 = x

if (n == 1 or n == 2) :

x = 1

return x

def PrintFN(m,n,i):

index = 0

flag = 0

while fib(i) < 10000 :

i = i + 1

count = i

i = 1

while i < count :

i = i + 1

if fib(i) >= m and fib(i) <= n :

flag = 1

index = index + 1

if index == 1 :

print(fib(i))

continue

print(fib(i))

if (flag == 0) :

print("No Fibonacci number")

m,n,i=input().split()

n=int(n)

m=int(m)

i=int(i)

PrintFN(m,n,i)

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值