The first few Fibonacci Numbers are 0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, ….
Note that 0 is considered as 0’th Fibonacci Number.
Input : M = 3, N = 6 Output : 2 Fib(3) = 2, Fib(6) = 8 GCD of above two numbers is 2 Input : M = 8, N = 12 Output : 3 Fib(8) = 21, Fib(12) = 144 GCD of above two numbers is 3
A Simple Solution is to follow below steps.
1) Find M’th Fibonacci Number.
2) Find N’th Fibonacci Number.
3) Return GCD of two numbers.
A Better Solution is based on below identity
GCD(Fib(M), Fib(N)) = Fib(GCD(M, N)) The above property holds because Fibonacci Numbers follow Divisibility Sequence, i.e., if M divides N, then Fib(M) also divides N. For example, Fib(3) = 2 and every third third Fibonacci Number is even. Source : Wiki
The steps are:
1) Find GCD of M and N. Let GCD be g.
2) Return Fib(g).
Below are implementations of above idea.
This article is contributed by Shubham Agrawal. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
- Cassini’s Identity
- Interesting facts about Fibonacci numbers
- GCD of more than two (or array) numbers
- Summation of GCD of all the pairs up to N
- n'th multiple of a number in Fibonacci Series
- Smallest number divisible by first n numbers
- Gaussian Filter Generation in C++
- Finding number of digits in n'th Fibonacci number
- Fibonacci Coding
- Euclidean algorithms (Basic and Extended)
- Program to find LCM of two numbers
- Program for nth Catalan Number
- How to check if a given number is Fibonacci number?
- Sieve of Eratosthenes
- Program for Fibonacci numbers
Improved By : nitin mittal