LCM of two numbers
It is a very common mathematical procedure .we can calculate l.c.m using prime factors from each number and find common prime factors from the two numbers. or else we can just calculate G.C.D ,with help of it we can calculate l.c.m.
Calculating L.C.M ,G.C.D will come in handy while solving competitive coding problems.
G.C.D can be calculated using Recursion.
Recursion: This is a process of calling the function itself repeatedly until base condition is met.
EXAMPLE
A()
{
if(base_condition())
else
A()
}
GCD procedure:
Greatest Common Divisor is the number which divides both the given numbers such that no other number greater than that number would not divide the two numbers perfectly.
Let the two numbers be 10,15
the factors of 10 are {1,2,5,10}
the factors of 15 are {1,3,5,15}
the maximum number which divides both the numbers perfectly is 5.
Program:
int GCD(int a,int b)
{
if(b==0)
return a;
return GCD(b,a%b);
}
This function returns G.C.D of two numbers.This function recursively calls itself until second parameter becomes 0 and then it returns
first parameter as G.C.D
L.C.M of two numbers a,b is a*b/G.C.D(a,b)