Finding Method:
The formula is:
GCD(a,b) * LCM(a,b) = a * b;
Therefore, LCM(a,b) = (a * b) / GCD(a,b);
Code and pitfalls:
int lcm(int a,int b)
{
return ( a * b ) / gcd ( a, b );
}
this code will work fine but for some cases it may overflow.So we should rewrite the (a*b)/gcd(a,b) part into a/gcd(a,b) * b .
What I use:
No comments:
Post a Comment