Given an integer **N**, the task is to find the **N ^{th}** Fibonacci number.

Input:N = 13

Output:144

Input:N = 19

Output:2584

**Approach:** The **N ^{th}** Fibonacci number can be found using the roots of the pell’s equation. Pells equation is generally of the form

**(x**.

^{2}) – n(y^{2}) = |1|Here, consider

**y**. Also, taken positive (+1) in the right-hand side.

^{2}= x, n = 1Now the equation becomes

**x**which is same as

^{2}– x = 1**x**.

^{2}– x – 1 = 0Here,

**{x = (p**is termed as

^{i}– q^{i}) / (p – q)}**N**term of the fibonaccci series where

^{th}**i = n – 1**and

**(p, q)**are the roots of the pell’s equation.

To find roots of general quadratic equation (a*x

^{2}+ b*x + c = 0).

x1 = [-b + math.sqrt(b^{2}– 4*a*c)] / 2*a

x2 = [-b – math.sqrt(b^{2}– 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 <bits/stdc++.h> ` `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 ` |

*chevron_right*

*filter_none*

## 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 ` |

*chevron_right*

*filter_none*

## 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)) ` |

*chevron_right*

*filter_none*

## 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.. ` |

*chevron_right*

*filter_none*

**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.

## Recommended Posts:

- Count of Fibonacci pairs which satisfy the given equation
- Check if a M-th fibonacci number divides N-th fibonacci number
- G-Fact 18 | Finding nth Fibonacci Number using Golden Ratio
- Find nth Fibonacci number using Golden ratio
- Check if sum of Fibonacci elements in an Array is a Fibonacci number or not
- Nth Even Fibonacci Number
- Program to find last two digits of Nth Fibonacci number
- Nth XOR Fibonacci number
- Program to find Nth odd Fibonacci Number
- Fast Doubling method to find the Nth Fibonacci number
- C/C++ Program for nth multiple of a number in Fibonacci Series
- Sum of nth terms of Modified Fibonacci series made by every pair of two arrays
- Find the Nth element of the modified Fibonacci series
- Nth Term of a Fibonacci Series of Primes formed by concatenating pairs of Primes in a given range
- Count of triplets that satisfy the given equation
- Program to balance the given Chemical Equation
- Find count of numbers from 0 to n which satisfies the given equation for a value K
- Count of Ordered Pairs (X, Y) satisfying the Equation 1/X + 1/Y = 1/N
- How to check if a given number is Fibonacci number?
- Finding number of digits in n'th Fibonacci number

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.