Python Program for n-th Fibonacci number

12 Jan, 2022
In mathematical terms, the sequence Fn of Fibonacci numbers is defined by the recurrence relation 
Fn = Fn-1 + Fn-2
With seed values 
F0 = 0 and F1 = 1.
Method 1 ( Use recursion ) :
Python3
# Function for nth Fibonacci number def Fibonacci(n):    if n<= 0:        print("Incorrect input")    # First Fibonacci number is 0    elif n == 1:        return 0    # Second Fibonacci number is 1    elif n == 2:        return 1    else:        return Fibonacci(n-1)+Fibonacci(n-2) # Driver Program print(Fibonacci(10)) # This code is contributed by Saket Modi
34
Method 2 ( Use Dynamic Programming ) : 
Python3
# Function for nth fibonacci number - Dynamic Programming# Taking 1st two fibonacci numbers as 0 and 1 FibArray = [0, 1] def fibonacci(n):    if n<0:        print("Incorrect input")    elif n<= len(FibArray):        return FibArray[n-1]    else:        temp_fib = fibonacci(n-1)+fibonacci(n-2)        FibArray.append(temp_fib)        return temp_fib # Driver Program print(fibonacci(9)) # This code is contributed by Saket Modi
21
Output: 
21
Method 3 ( Use Dynamic Programming with Space Optimization) : 
Python3
# Function for nth fibonacci number - Space Optimisation# Taking 1st two fibonacci numbers as 0 and 1 def fibonacci(n):    a = 0    b = 1    if n < 0:        print("Incorrect input")    elif n == 0:        return a    elif n == 1:        return b    else:        for i in range(2, n):            c = a + b            a = b            b = c        return b # Driver Program print(fibonacci(9)) # This code is contributed by Saket Modi
21
Output: 
21
Method 4 ( Using Arrays ) : 
Python3
# creating an array in the function to find the nth number in fibonacci series. [0, 1, 1, ...] def fibonacci (n):   if n<= 0:     return "Incorrect Output"   data = [0, 1]   if n > 2:        for i in range (2, n):            data.append(data[i-1] + data[i-2])    return data[n-1] # Driver Program print(fibonacci(9)) # This Code is contributed by Prasun Parate (prasun_parate)
Output :
144
Explanation:
[0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144]As we know that the Fibonacci series is the sum of the previous two terms, so if we enter 12 as the input in the program, so we should get 144 as the output. And that is what is the result. 
Method 5 ( Using Direct Formula ) : 
The formula for finding the n-th Fibonacci number is as follows:
Python3
# To find the n-th Fibonacci Number using formulafrom math import sqrt# import square-root method from math librarydef nthFib(n):    res = (((1+sqrt(5))**n)-((1-sqrt(5)))**n)/(2**n*sqrt(5))    # compute the n-th fibonacci number    print(int(res),'is',str(n)+'th fibonacci number')    # format and print the number     # driver codenthFib(12) # This code is contributed by Kush Mehta
144 is 12th fibonacci number
Find nth fibonacci no. using recursive technique. | GeeksforGeeks - YouTubeGeeksforGeeks530K subscribersFind nth fibonacci no. using recursive technique. | GeeksforGeeksWatch laterShareCopy linkInfoShoppingTap to unmuteIf playback doesn't begin shortly, try restarting your device.More videosMore videosYou're signed outVideos you watch may be added to the TV's watch history and influence TV recommendations. To avoid this, cancel and sign in to YouTube on your computer.CancelConfirmSwitch cameraShareInclude playlistAn error occurred while retrieving sharing information. Please try again later.Watch on0:000:000:00 / 2:57•Live•<div class="player-unavailable"><h1 class="message">An error occurred.</h1><div class="submessage"><a href="https://www.youtube.com/watch?v=4GMh0MfCiqE" target="_blank">Try watching this video on www.youtube.com</a>, or enable JavaScript if it is disabled in your browser.</div></div>
Please refer complete article on Program for Fibonacci numbers for more details! 
prasun_parate
varshagumber28
akshaysingh98088
arorakashish0911
kushmehta170
amartyaghoshgfg
Fibonacci
Python Programs
Fibonacci
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Python program to add two numbers
Python Program for factorial of a number
Python program to find second largest number in a list
Iterate over characters of a string in Python
Python | Convert set into a list
Appending to list in Python dictionary
Python | Convert a list into a tuple
Python Program for Bubble Sort
Add a key:value pair to dictionary in Python
Python | Check if a variable is string