VOOZH about

URL: https://www.geeksforgeeks.org/dsa/program-for-nth-fibonacci-number/

⇱ Nth Fibonacci Number - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

Nth Fibonacci Number

Last Updated : 2 Apr, 2026

Given a positive integer n, find the nth Fibonacci number.

The Fibonacci series is a sequence where a term is the sum of previous two terms. The first two terms of the Fibonacci sequence are 0 followed by 1. The Fibonacci sequence: 0, 1, 1, 2, 3, 5, 8, 13, 21.

Example:

Input: n = 2
Output: 1
Explanation: 1 is the 2nd number of Fibonacci series.

Input: n = 5
Output: 5
Explanation: 5 is the 5th number of Fibonacci series.

[Naive Approach] Using Recursion

We can use recursion to solve this problem because any Fibonacci number n depends on previous two Fibonacci numbers. Therefore, this approach repeatedly breaks down the problem until it reaches the base cases.

Recurrence relation:

  • Base case: F(n) = n, when n = 0 or n = 1
  • Recursive case: F(n) = F(n-1) + F(n-2) for n>1

Output
5

Time Complexity: O(2n), because each state requires answers from previous two states, and thus calls the function for both of those states recursively.
Auxiliary Space: O(n), due to recursion stack

[Expected Approach-1] Memoization Approach

The idea is to optimize the recursive solution by storing the results of already solved subproblems in a memoization table. Whenever the same subproblem appears again, we reuse the stored result instead of recomputing it. This avoids repeated calculations and improves the overall efficiency, reducing the time complexity from exponential to linear.

👁 introduction_to_recursion

Output
5

Time Complexity: O(n), each Fibonacci number is calculated only one time,
Auxiliary Space: O(n), for dp table.

[Expected Approach-2] Bottom-Up Approach

In this approach, we are iteratively calculating the answers from the bottom up, beginning with the smallest subproblems, the first two Fibonacci numbers 0 and 1. Using these initial values, we generate the next numbers one by one by adding the previous two results. Each value is calculated only once and reused later, so no repeated work is done. This method avoids recursion and makes the solution simple and efficient with linear time complexity.

  • Start with the base values fib(0) = 0 and fib(1) = 1.
  • Initialize variables or an array to store these initial results.
  • Iterate from index 2 up to n.
  • At each step compute fib(i) as fib(i-1) + fib(i-2).
  • Continue until the nth Fibonacci number is obtained.

Output
5

Time Complexity: O(n), each fibonacci number is calculated only one time,
Auxiliary Space: O(n), for dp table.

[Expected Approach-3] Bottom Up Space Optimized Approach - O(n) Time and O(1) Space

In this approach, we store the results of the previous two states, and as we move forward, we update them to compute the next values. This means keeping track of the last two Fibonacci numbers and using them to calculate the next one, efficiently covering all states without needing extra space for a full DP table.


Output
5

Using Matrix Exponentiation - O(log(n)) time and O(log(n)) space

In this approach, we observe that Fibonacci number is the sum of previous two Fibonacci numbers. this could be done by adding numbers repeatedly or use loops or recursion, which takes time. But with matrix exponentiation, we can calculateFibonacci numbers much faster by working with matrices. There's a special matrix (transformation matrix) that represents how Fibonacci numbers work.

It looks like this: This matrix captures the Fibonacci relationship. If we multiply this matrix by itself multiple times, it can give us Fibonacci numbers.

To find the Nth Fibonacci number we need to multiply transformation matrix (n-1) times, the matrix equation for the Fibonacci sequence looks like:

After raising the transformation matrix to the power n - 1, the top-left element F(n) will give the nth Fibonacci number.


Output
5

Time Complexity: O(log(n), We have used exponentiation by squaring, which reduces the number of matrix multiplications to O(log n), because with each recursive call, the power is halved.
Auxiliary Space: O(log n), due to the recursion stack.

[Other Approach] Using Golden ratio

The nth Fibonacci number can be found using theGolden Ratio, which is approximately = . The intuition behind this method is based on Binet's formula, which expresses the nth Fibonacci number directly in terms of the Golden Ratio.

Binet's Formula: The nth Fibonacci number F(n) can be calculated using the formula:

For more detail for this approach, please refer to this article.

Related Articles:

Comment