0

I want to find the next fibonacci number in the fibonacci series. Like if i entered 9 then the function should return 13 which is next fibonacci number.

i could not understood logic how it can be implemented. Just messing with fibonacci program.

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.

1 Answers1

0

You are misinterpreting the number 'n'. In your program you ask for the n'th number which is not equal to for instance the number 9. So your program will work if you want to calculate the n'th fibonacci number. To get what you want you would have to do something like:

    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))
Arthur
  • 41
  • 4