Tag Archives: gcd

Algorithms: LCM and GCD

Least Common Multiple: lcm(a,b) = abs(a*b) / gcd(a,b) = abs(a) / gcd(a,b) * abs(b) Greatest common divisor: gcd(a,b) = gcd(b, a%b); Code: int gcd(int a, int b) { if (b == 0) return a; return gcd(b, a%b); } Advertisements

Posted in Coding | Tagged , , | Leave a comment