我想得到下一个斐波那契数
i want to get the next fibonacci number
我想在斐波那契数列中找到下一个斐波那契数。就像如果我输入 9 那么函数应该 return 13 这是下一个斐波那契数。
我无法理解如何实现它的逻辑。只是在搞乱斐波那契程序。
def Fibonacci(n):
if n<0:
print("Incorrect input")
elif n==1:
return 0
elif n==2:
return 1
else:
return Fibonacci(n-1)+Fibonacci(n-2)
print(Fibonacci(9))
if i entered 9 then the function should return 13 which is next fibonacci number.
您误解了数字 'n'。
在您的程序中,您要求第 n 个不等于数字 9 的数字。
所以如果你想计算第 n 个斐波那契数,你的程序就可以工作。
要得到你想要的东西,你必须做类似的事情:
def Fibonnacci(givenNumber):
number1 = 1
number2 = 1
nextFibo = number1 + number2
while nextFibo <= givenNumber:
number1 = number2
number2 = nextFibo
nextFibo = number1 + number2
return nextFibo
print(Fibonnacci(9))
我想在斐波那契数列中找到下一个斐波那契数。就像如果我输入 9 那么函数应该 return 13 这是下一个斐波那契数。
我无法理解如何实现它的逻辑。只是在搞乱斐波那契程序。
def Fibonacci(n):
if n<0:
print("Incorrect input")
elif n==1:
return 0
elif n==2:
return 1
else:
return Fibonacci(n-1)+Fibonacci(n-2)
print(Fibonacci(9))
if i entered 9 then the function should return 13 which is next fibonacci number.
您误解了数字 'n'。 在您的程序中,您要求第 n 个不等于数字 9 的数字。 所以如果你想计算第 n 个斐波那契数,你的程序就可以工作。 要得到你想要的东西,你必须做类似的事情:
def Fibonnacci(givenNumber):
number1 = 1
number2 = 1
nextFibo = number1 + number2
while nextFibo <= givenNumber:
number1 = number2
number2 = nextFibo
nextFibo = number1 + number2
return nextFibo
print(Fibonnacci(9))