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.
  1. def multiply(F, M):
  2.     x = F[0][0] * M[0][0] + F[0][1] * M[1][0]
  3.     y = F[0][0] * M[0][1] + F[0][1] * M[1][1]
  4.     z = F[1][0] * M[0][0] + F[1][1] * M[1][0]
  5.     w = F[1][0] * M[0][1] + F[1][1] * M[1][1]
  6.  
  7.     F[0][0] = x
  8.     F[0][1] = y
  9.     F[1][0] = z
  10.     F[1][1] = w
  11.  
  12.  
  13. def power(F, n):
  14.     if n <= 1:
  15.         return
  16.  
  17.     M = [[1, 1],
  18.          [1, 0]]
  19.  
  20.     power(F, n // 2)
  21.     multiply(F, F)
  22.  
  23.     if n % 2 != 0:
  24.         multiply(F, M)
  25.  
  26.  
  27. def fib(n):
  28.     if n == 0:
  29.         return 0
  30.  
  31.     F = [[1, 1],
  32.          [1, 0]]
  33.  
  34.     power(F, n - 1)
  35.     return F[0][0]
  36.  
  37.  
  38. # MAIN LOOP
  39. while True:
  40.     print("\n============= Find the Nth Fibonacci Number =============\n")
  41.  
  42.     try:
  43.         n = int(input("Enter the value of n: "))
  44.  
  45.         if n < 0:
  46.             print("Please enter a non-negative integer.")
  47.             continue
  48.  
  49.         result = fib(n)
  50.  
  51.         print(f"\nThe {n}th Fibonacci number is: {result}")
  52.  
  53.     except ValueError:
  54.         print("Invalid input. Please enter an integer.")
  55.         continue
  56.  
  57.     # Try Again Option
  58.     opt = input("\nDo you want to try again? (yes/no): ").strip().lower()
  59.  
  60.     if opt == 'no':
  61.         print("Exiting program...")
  62.         break
  63.     elif opt != 'yes':
  64.         print("Invalid choice. Exiting program...")
  65.         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

Python Tutorials