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.
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.
- Find the GCD of N Fibonacci Numbers with given Indices
- Pair of integers having least GCD among all given pairs having GCD exceeding K
- GCD of elements occurring Fibonacci number of times in an Array
- Check if a M-th fibonacci number divides N-th fibonacci number
- Check if sum of Fibonacci elements in an Array is a Fibonacci number or not
- Print N lines of 4 numbers such that every pair among 4 numbers has a GCD K
- Sum and product of K smallest and largest Fibonacci numbers in the array
- GCD of two numbers formed by n repeating x and y times
- Find two numbers whose sum and GCD are given
- Count Fibonacci numbers in given range in O(Log n) time and O(1) space
- Prime numbers and Fibonacci
- Largest and smallest Fibonacci numbers in an Array
- Numbers with a Fibonacci difference between Sum of digits at even and odd positions in a given range
- GCD of two numbers when one of them can be very large
- Count pairs of natural numbers with GCD equal to given number
- GCD of more than two (or array) numbers
- Finding LCM of more than two (or array) numbers without using GCD
- Queries for GCD of all numbers of an array except elements in a given range
- C++ Program for GCD of more than two (or array) numbers
- Java Program for GCD of more than two (or array) numbers