Nth Fibonacci number using Pell's equation Last Updated : 19 Oct, 2022 Summarize Comments Improve Suggest changes Share Like Article Like Report Given an integer N, the task is to find the Nth Fibonacci number. Examples: 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 <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 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 <script> // Javascript implementation of the approach // Function to return the // nth fibonacci number function fib(n) { // Assign roots of the pell's // equation to p and q let p = ((1 + Math.sqrt(5)) / 2); let q = ((1 - Math.sqrt(5)) / 2); let i = n - 1; let x = parseInt((Math.pow(p, i) - Math.pow(q, i)) / (p - q)); return x; } // Driver code let n = 5; document.write(fib(n)); </script> Output: 3 Time complexity: O(logn) Auxiliary Space: O(1) Comment More infoAdvertise with us Next Article Sum of Fibonacci Numbers in a range W webbdays Follow Improve Article Tags : DSA Fibonacci Numbers Practice Tags : FibonacciNumbers Similar Reads Nth Fibonacci Number using Binet's Formula Given a number n, print n-th Fibonacci Number, using Binet's Formula. Examples: Input: n = 5Output: 1Explanation: The 5th Fibonacci number in the sequence (0, 1, 1, 2, 3, 5, ...) is 1.Input: n = 9Output: 34Explanation: The 9th Fibonacci number in the sequence (0, 1, 1, 2, 3, 5, 8, 13, 21, 34, ...) i 4 min read Nth Even Fibonacci Number Given a value n, find the n'th even Fibonacci Number.Examples : Input n = 3Output 34 Explanation The first 3 even Fibonacci numbers are 0, 2, 8, 34, 144, with the 3rd being 34.Input n = 4 Output 144 Explanation The first 4 even Fibonacci numbers are 0, 2, 8, 34, 144, with the 4th being 144.[Naive Ap 5 min read Nth Fibonacci Number Given a positive integer n, the task is to find the nth Fibonacci number.The Fibonacci sequence is a sequence where the next term is the sum of the 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, 21Example:Input: 15+ min read Even Fibonacci Numbers Sum Given a limit, find the sum of all the even-valued terms in the Fibonacci sequence below given limit.The first few terms of Fibonacci Numbers are, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233 ,... (Even numbers are highlighted).Examples : Input : limit = 8 Output : 10 Explanation : 2 + 8 = 10 Inpu 6 min read Sum of Fibonacci Numbers in a range Given a range [l, r], the task is to find the sum fib(l) + fib(l + 1) + fib(l + 2) + ..... + fib(r) where fib(n) is the nth Fibonacci number. Examples: Input: l = 2, r = 5 Output: 11 fib(2) + fib(3) + fib(4) + fib(5) = 1 + 2 + 3 + 5 = 11 Input: l = 4, r = 8 Output: 50 Naive approach: Simply calculat 8 min read Find nth Fibonacci number using Golden ratio Fibonacci series = 0, 1, 1, 2, 3, 5, 8, 13, 21, 34, ........Different methods to find nth Fibonacci number are already discussed. Another simple way of finding nth Fibonacci number is using golden ratio as Fibonacci numbers maintain approximate golden ratio till infinite. Golden ratio: \varphi ={\fr 6 min read Like