Number Theory --- lcm and gcd

cd that is the greatest common divisor, lcm that is the least common multiple.

Firstly, a × b = gcd × lcm

Proof: Let gcd (a, b) = k , a = xk, b = yk, then X = B × A Y K K, and X = LCM Y K, so that A B = GCD * LCM.

Therefore, the method can seek to find gcd lcm, gcd is the sake Euclidean algorithm, also called the Euclidean algorithm, the core of the gcd (m, n) = gcd (n, m% n)
recursive:
'' '
LL GCD (LL A, LL B) {
return B GCD (B, A% B): A;?
}
'' '
the while loop:
' ''
LL GCD (LL A, LL B) {
LL T;
the while (B) {
T = B;
B = A% B;
A = T;
}
return A;
}
'' '

Guess you like

Origin www.cnblogs.com/ACMerLwy/p/11647373.html