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.
Don’t stop now and take your learning to the next level. Learn all the important concepts of Data Structures and Algorithms with the help of the most trusted course: DSA Self Paced. Become industry ready at a student-friendly price.
- Even Fibonacci Numbers Sum
- Non Fibonacci Numbers
- Sum of Fibonacci Numbers
- Sum of squares of Fibonacci numbers
- Sum of Fibonacci Numbers in a range
- The Magic of Fibonacci Numbers
- Find the sum of first N odd Fibonacci numbers
- Prime numbers and Fibonacci
- C Program for Fibonacci numbers
- Alternate Fibonacci Numbers
- Program for Fibonacci numbers
- Interesting facts about Fibonacci numbers
- Sum of numbers in the Kth level of a Fibonacci triangle
- Large Fibonacci Numbers in Java
- Sum of all Non-Fibonacci numbers in a range for Q queries
- Remove all the fibonacci numbers from the given array
- Program to print first n Fibonacci Numbers | Set 1
- Find two Fibonacci numbers whose sum can be represented as N
- Find the GCD of N Fibonacci Numbers with given Indices
- Sum of Fibonacci Numbers with alternate negatives