Program to find LCM of two Fibonacci Numbers
Given here are two positive numbers a and b. The task is to print the least common multiple of a’th and b’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 : a = 3, b = 12
Output : 144
Input : a = 8, b = 37
Output : 507314157
Approach: The simple solution of the problem is,
- Find the a’th fibonacci number.
- Find the b’th fibonacci number.
- Find their GCD, and with the help of the GCD find their LCM. The relation is LCM(a, b) = (a x b) / GCD(a, b) (Please refer here).
Below is the implementation of the above approach:
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 a, int b)
{
if (a == 0)
return b;
return gcd(b % a, a);
}
int findLCMFibonacci( int a, int b)
{
return (fib(a) * fib(b)) / fib(gcd(a, b));
}
int main()
{
int a = 3, b = 12;
cout << findLCMFibonacci(a, b);
return 0;
}
|
Java
import java.util.*;
class GFG
{
static int MAX = 1000 ;
static int [] f = new int [MAX];
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 = 0 ;
if ((n & 1 ) != 0 )
k = (n + 1 ) / 2 ;
else
k = n / 2 ;
if ((n & 1 ) != 0 )
f[n] = (fib(k) * fib(k) +
fib(k - 1 ) * fib(k - 1 ));
else
f[n] = ( 2 * fib(k - 1 ) +
fib(k)) * fib(k);
return f[n];
}
static int gcd( int a, int b)
{
if (a == 0 )
return b;
return gcd(b % a, a);
}
static int findLCMFibonacci( int a, int b)
{
return (fib(a) * fib(b)) / fib(gcd(a, b));
}
public static void main(String args[])
{
int a = 3 , b = 12 ;
System.out.println(findLCMFibonacci(a, b));
}
}
|
Python3
MAX = 1000
f = [ 0 ] * MAX
def fib(n):
if (n = = 0 ):
return 0
if (n = = 1 or n = = 2 ):
f[n] = 1
return f[n]
if (f[n]):
return f[n]
k = (n + 1 ) / / 2 if (n & 1 ) else n / / 2
if (n & 1 ):
f[n] = (fib(k) * fib(k) +
fib(k - 1 ) * fib(k - 1 ))
else :
f[n] = ( 2 * fib(k - 1 ) + fib(k)) * fib(k)
return f[n]
def gcd(a, b):
if (a = = 0 ):
return b
return gcd(b % a, a)
def findLCMFibonacci(a, b):
return (fib(a) * fib(b)) / / fib(gcd(a, b))
if __name__ = = "__main__" :
a = 3
b = 12
print (findLCMFibonacci(a, b))
|
C#
using System;
class GFG
{
static int MAX = 1000;
static int [] f = new int [MAX];
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 = 0;
if ((n & 1) != 0)
k = (n + 1) / 2;
else
k = n / 2;
if ((n & 1 ) != 0)
f[n] = (fib(k) * fib(k) +
fib(k - 1) * fib(k - 1));
else
f[n] = (2 * fib(k - 1) +
fib(k)) * fib(k);
return f[n];
}
static int gcd( int a, int b)
{
if (a == 0)
return b;
return gcd(b % a, a);
}
static int findLCMFibonacci( int a, int b)
{
return (fib(a) * fib(b)) / fib(gcd(a, b));
}
static void Main()
{
int a = 3, b = 12;
Console.WriteLine(findLCMFibonacci(a, b));
}
}
|
PHP
<?php
$GLOBALS [ 'MAX' ] = 1000;
$GLOBALS [ 'f' ] = array ();
for ( $i = 0; $i < $GLOBALS [ 'MAX' ]; $i ++)
$GLOBALS [ 'f' ][ $i ] = 0;
function fib( $n )
{
if ( $n == 0)
return 0;
if ( $n == 1 || $n == 2)
return ( $GLOBALS [ 'f' ][ $n ] = 1);
if ( $GLOBALS [ 'f' ][ $n ])
return $GLOBALS [ 'f' ][ $n ];
$k = ( $n & 1) ? ( $n + 1) / 2 : $n / 2;
$GLOBALS [ 'f' ][ $n ] = ( $n & 1) ?
(fib( $k ) * fib( $k ) +
fib( $k - 1) * fib( $k - 1)) :
(2 * fib( $k - 1) + fib( $k )) * fib( $k );
return $GLOBALS [ 'f' ][ $n ];
}
function gcd( $a , $b )
{
if ( $a == 0)
return $b ;
return gcd( $b % $a , $a );
}
function findLCMFibonacci( $a , $b )
{
return (fib( $a ) * fib( $b )) /
fib(gcd( $a , $b ));
}
$a = 3;
$b = 12;
echo findLCMFibonacci( $a , $b );
?>
|
Javascript
<script>
let MAX = 1000;
let f = new Array(MAX);
for (let i=0;i<MAX;i++)
{
f[i]=0;
}
function fib(n)
{
if (n == 0)
return 0;
if (n == 1 || n == 2)
return (f[n] = 1);
if (f[n] != 0)
return f[n];
let k = 0;
if ((n & 1) != 0)
k = (n + 1) / 2;
else
k = n / 2;
if ((n & 1 ) != 0)
f[n] = (fib(k) * fib(k) +
fib(k - 1) * fib(k - 1));
else
f[n] = (2 * fib(k - 1) +
fib(k)) * fib(k);
return f[n];
}
function gcd(a,b)
{
if (a == 0)
return b;
return gcd(b % a, a);
}
function findLCMFibonacci(a,b)
{
return (fib(a) * fib(b)) / fib(gcd(a, b));
}
let a = 3, b = 12;
document.write(findLCMFibonacci(a, b));
</script>
|
Time Complexity: O(2n)
Auxiliary Space: O(MAX)
Last Updated :
23 Jun, 2022
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...