How to Find the Nth Fibonacci Number in Python
In this tutorial, we will learn how to program "How to Find the Nth Fibonacci Number in Python". The objective is to find the Nth Fibonacci number. This tutorial will guide you step by step through the process of finding the Nth Fibonacci number. By the end of this tutorial, you will have a solid understanding of how to implement this solution effectively in Python, helping you strengthen your problem-solving abilities and improve your coding skills.
This topic is straightforward and easy to understand. By following the instructions provided, you will be able to complete it with ease. The program will guide you step by step through the process of finding the Nth Fibonacci number. So, let’s dive into the coding process!
Getting Started:
First you will have to download & install the Python IDLE's, here's the link for the Integrated Development And Learning Environment for Python https://www.python.org/downloads/.
Creating Main Function
This is the main function of the application. The following code will display a simple GUI in terminal console that will display program. To do this, simply copy and paste these blocks of code into the IDLE text editor.- def multiply(F, M):
- x = F[0][0] * M[0][0] + F[0][1] * M[1][0]
- y = F[0][0] * M[0][1] + F[0][1] * M[1][1]
- z = F[1][0] * M[0][0] + F[1][1] * M[1][0]
- w = F[1][0] * M[0][1] + F[1][1] * M[1][1]
- F[0][0] = x
- F[0][1] = y
- F[1][0] = z
- F[1][1] = w
- def power(F, n):
- if n <= 1:
- return
- M = [[1, 1],
- [1, 0]]
- power(F, n // 2)
- multiply(F, F)
- if n % 2 != 0:
- multiply(F, M)
- def fib(n):
- if n == 0:
- return 0
- F = [[1, 1],
- [1, 0]]
- power(F, n - 1)
- return F[0][0]
- # MAIN LOOP
- while True:
- print("\n============= Find the Nth Fibonacci Number =============\n")
- try:
- n = int(input("Enter the value of n: "))
- if n < 0:
- print("Please enter a non-negative integer.")
- continue
- result = fib(n)
- print(f"\nThe {n}th Fibonacci number is: {result}")
- except ValueError:
- print("Invalid input. Please enter an integer.")
- continue
- # Try Again Option
- opt = input("\nDo you want to try again? (yes/no): ").strip().lower()
- if opt == 'no':
- print("Exiting program...")
- break
- elif opt != 'yes':
- print("Invalid choice. Exiting program...")
- break
This program calculates the Nth Fibonacci number using matrix exponentiation, an efficient approach that reduces computation time compared to the traditional recursive method. It uses 2×2 matrix multiplication and exponentiation by squaring to compute powers of the Fibonacci transformation matrix. The main loop allows user input for the value of n, validates it, and repeatedly computes and displays the corresponding Fibonacci number until the user chooses to exit.
Output:
There you have it we successfully created How to Find the Nth Fibonacci Number in Python. I hope that this simple tutorial help you to what you are looking for. For more updates and tutorials just kindly visit this site. Enjoy Coding!
More Tutorials for Python Language