GCD and Fibonacci Numbers
Last Updated :
03 Jun, 2022
You are given two positive numbers M and N. The task is to print greatest common divisor of M’th and N’th Fibonacci Numbers.
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.
Examples:
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.
C++
#include <bits/stdc++.h>
using namespace std;
const int MAX = 1000;
int f[MAX] = { 0 };
int fib( int n)
{
if (n == 0)
return 0;
if (n == 1 || n == 2)
return (f[n] = 1);
if (f[n])
return f[n];
int k = (n & 1) ? (n + 1) / 2 : n / 2;
f[n] = (n & 1)
? (fib(k) * fib(k) + fib(k - 1) * fib(k - 1))
: (2 * fib(k - 1) + fib(k)) * fib(k);
return f[n];
}
int gcd( int M, int N)
{
if (M == 0)
return N;
return gcd(N % M, M);
}
int findGCDofFibMFibN( int M, int N)
{
return fib(gcd(M, N));
}
int main()
{
int M = 3, N = 12;
cout << findGCDofFibMFibN(M, N);
return 0;
}
|
C
#include <stdio.h>
const int MAX = 1000;
int fib( int n)
{
int arr[MAX];
for ( int i = 0; i < MAX; i++)
arr[i] = 0;
if (n == 0)
return 0;
if (n == 1 || n == 2)
return (arr[n] = 1);
if (arr[n])
return arr[n];
int k = (n & 1) ? (n + 1) / 2 : n / 2;
arr[n]
= (n & 1)
? (fib(k) * fib(k) + fib(k - 1) * fib(k - 1))
: (2 * fib(k - 1) + fib(k)) * fib(k);
return arr[n];
}
int gcd( int M, int N)
{
if (M == 0)
return N;
return gcd(N % M, M);
}
int findGCDofFibMFibN( int M, int N)
{
return fib(gcd(M, N));
}
int main()
{
int M = 3, N = 12;
printf ( "%d" , findGCDofFibMFibN(M, N));
return 0;
}
|
Java
class gcdOfFibonacci
{
static final int MAX = 1000 ;
static int [] f;
gcdOfFibonacci()
{
f = new int [MAX];
}
private static int fib( int n)
{
if (n == 0 )
return 0 ;
if (n == 1 || n == 2 )
return (f[n] = 1 );
if (f[n]!= 0 )
return f[n];
int k = ((n & 1 )== 1 )? (n+ 1 )/ 2 : n/ 2 ;
f[n] = ((n & 1 )== 1 )? (fib(k)*fib(k) + fib(k- 1 )*fib(k- 1 ))
: ( 2 *fib(k- 1 ) + fib(k))*fib(k);
return f[n];
}
private static int gcd( int M, int N)
{
if (M == 0 )
return N;
return gcd(N%M, M);
}
static int findGCDofFibMFibN( int M, int N)
{
return fib(gcd(M, N));
}
public static void main(String[] args)
{
gcdOfFibonacci obj = new gcdOfFibonacci();
int M = 3 , N = 12 ;
System.out.println(findGCDofFibMFibN(M, N));
}
}
|
Python3
MAX = 1000
f = [ 0 for i in range ( MAX )]
def fib(n):
if (n = = 0 ):
return 0
if (n = = 1 or n = = 2 ):
f[n] = 1
if (f[n]):
return f[n]
k = (n + 1 ) / / 2 if (n & 1 ) else n / / 2
f[n] = (fib(k) * fib(k) + fib(k - 1 ) * fib(k - 1 )) if (n & 1 ) else (( 2 *
fib(k - 1 ) + fib(k)) * fib(k))
return f[n]
def gcd(M, N):
if (M = = 0 ):
return N
return gcd(N % M, M)
def findGCDofFibMFibN(M, N):
return fib(gcd(M, N))
M = 3
N = 12
print (findGCDofFibMFibN(M, N))
|
C#
using System;
class gcdOfFibonacci {
static int MAX = 1000;
static int []f;
gcdOfFibonacci()
{
f = new int [MAX];
}
private static int fib( int n)
{
if (n == 0)
return 0;
if (n == 1 || n == 2)
return (f[n] = 1);
if (f[n]!=0)
return f[n];
int k = ((n & 1)==1)? (n+1)/2 : n/2;
f[n] = ((n & 1) == 1) ? (fib(k) * fib(k) +
fib(k - 1) * fib(k - 1)) :
(2 * fib(k - 1) + fib(k)) * fib(k);
return f[n];
}
private static int gcd( int M, int N)
{
if (M == 0)
return N;
return gcd(N%M, M);
}
static int findGCDofFibMFibN( int M, int N)
{
return fib(gcd(M, N));
}
public static void Main()
{
new gcdOfFibonacci();
int M = 3, N = 12;
Console.Write(findGCDofFibMFibN(M, N));
}
}
|
PHP
<?php
$MAX = 1000;
$f = array_fill (0, $MAX , 0);
function fib( $n )
{
global $f ;
if ( $n == 0)
return 0;
if ( $n == 1 or $n == 2)
$f [ $n ] = 1;
if ( $f [ $n ])
return $f [ $n ];
$k = ( $n & 1) ? ( $n + 1) / 2 : $n / 2;
$f [ $n ] = ( $n & 1) ?
(fib( $k ) * fib( $k ) + fib( $k - 1) * fib( $k - 1)) :
((2 * fib( $k - 1) + fib( $k )) * fib( $k ));
return $f [ $n ];
}
function gcd( $M , $N )
{
if ( $M == 0)
return $N ;
return gcd( $N % $M , $M );
}
function findGCDofFibMFibN( $M , $N )
{
return fib(gcd( $M , $N ));
}
$M = 3;
$N = 12;
print (findGCDofFibMFibN( $M , $N ))
?>
|
Javascript
<script>
const MAX = 1000;
var f = [...Array(MAX)];
f.fill(0);
function fib(n) {
if (n == 0) return 0;
if (n == 1 || n == 2) return (f[n] = 1);
if (f[n]) return f[n];
var k = n & 1 ? (n + 1) / 2 : n / 2;
f[n] =
n & 1
? fib(k) * fib(k) + fib(k - 1) * fib(k - 1)
: (2 * fib(k - 1) + fib(k)) * fib(k);
return f[n];
}
function gcd(M, N) {
if (M == 0) return N;
return gcd(N % M, M);
}
function findGCDofFibMFibN(M, N) {
return fib(gcd(M, N));
}
var M = 3,
N = 12;
document.write(findGCDofFibMFibN(M, N));
</script>
|
Output:
2
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...