Related Articles

# Nth Fibonacci number using Pell’s equation

• Last Updated : 14 Sep, 2021

Given an integer N, the task is to find the Nth Fibonacci number.

Input: N = 13
Output: 144
Input: N = 19
Output: 2584

Approach: The Nth Fibonacci number can be found using the roots of the pell’s equation. Pells equation is generally of the form (x2) – n(y2) = |1|
Here, consider y2 = x, n = 1. Also, taken positive (+1) in the right-hand side.
Now the equation becomes x2 – x = 1 which is same as x2 – x – 1 = 0
Here, {x = (pi – qi) / (p – q)} is termed as Nth term of the fibonacci series where i = n – 1 and (p, q) are the roots of the pell’s equation.

To find roots of general quadratic equation (a*x2 + b*x + c = 0).
x1 = [-b + math.sqrt(b2 – 4*a*c)] / 2*a
x2 = [-b – math.sqrt(b2 – 4*a*c)] / 2*a
i.e.
p = (1 + math.sqrt(5)) / 2
q = (1 – math.sqrt(5)) / 2

Below is the implementation of the above approach:

## C++

 `// C++ implementation of the approach``#include ``using` `namespace` `std;` `// Function to return the``// nth fibonacci number``int` `fib(``int` `n)``{``    ``// Assign roots of the pell's``    ``// equation to p and q``    ``double` `p = ((1 + ``sqrt``(5)) / 2);``    ``double` `q = ((1 - ``sqrt``(5)) / 2);``    ``int` `i = n - 1;``    ``int` `x = (``int``) ((``pow``(p, i) -``                    ``pow``(q, i)) / (p - q));``    ``return` `x;``}` `// Driver code``int` `main()``{``    ``int` `n = 5;``    ``cout << fib(n);``}` `// This code is contributed by PrinciRaj1992`

## Java

 `// Java implementation of the approach``class` `GFG``{``    ` `// Assign roots of the pell's``// equation to p and q``static` `double` `p = ((``1` `+ Math.sqrt(``5``)) / ``2``);``static` `double` `q = ((``1` `- Math.sqrt(``5``)) / ``2``);` `// Function to return the``// nth fibonacci number``static` `int` `fib(``int` `n)``{``    ``int` `i = n - ``1``;``    ``int` `x = (``int``) ((Math.pow(p, i) -``                    ``Math.pow(q, i)) / (p - q));``    ``return` `x;``}` `// Driver code``public` `static` `void` `main(String[] args)``{``    ``int` `n = ``5``;``    ``System.out.println(fib(n));``}``}` `// This code is contributed by 29AjayKumar`

## Python3

 `# Python3 implementation of the approach``import` `math` `# Assign roots of the pell's``# equation to p and q``p ``=` `(``1` `+` `math.sqrt(``5``)) ``/` `2``q ``=` `(``1` `-` `math.sqrt(``5``)) ``/` `2` `# Function to return the``# nth fibonacci number``def` `fib(n):``    ``i ``=` `n ``-` `1``    ``x ``=` `(p``*``*``i ``-` `q``*``*``i) ``/` `(p ``-` `q)``    ``return` `int``(x)` `# Driver code``n ``=` `5``print``(fib(n))`

## C#

 `// C# implementation of the approach``using` `System;` `class` `GFG``{``        ` `// Assign roots of the pell's``// equation to p and q``static` `double` `p = ((1 + Math.Sqrt(5)) / 2);``static` `double` `q = ((1 - Math.Sqrt(5)) / 2);` `// Function to return the``// nth fibonacci number``static` `int` `fib(``int` `n)``{``    ``int` `i = n - 1;``    ``int` `x = (``int``) ((Math.Pow(p, i) -``                    ``Math.Pow(q, i)) / (p - q));``    ``return` `x;``}` `// Driver code``static` `public` `void` `Main ()``{``    ``int` `n = 5;``    ``Console.Write(fib(n));``}``}` `// This code is contributed by @ajit..`

## Javascript

 ``
Output:
`3`

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.  To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.

In case you wish to attend live classes with experts, please refer DSA Live Classes for Working Professionals and Competitive Programming Live for Students.

My Personal Notes arrow_drop_up