Euclidean algorithm
Euclidean algorithm, also known as the greatest common divisor method, is used to calculate two integers, a, b, and so on. Its computational principle relies on the following theorem:
Theorem: gcd (b) = gcd (b,a mod b)
Proof: A can be expressed as A = kb + R, then r = a mod b
Assuming D is a number of conventions for a, B, there are
D|a, d|b, and r = a-kb, so d|r
So d is the number of conventions (B,a mod b)
Assuming D is the number of conventions (B,a mod b), then
D | B, D |r, but a = KB +r
So d is also the number of conventions (A, B)
therefore (b) and (b,a mod b) The number of conventions is the same, and their greatest common divisor are necessarily equal, to be certified
Euclid's algorithm is based on this principle, and its algorithm is described in terms of language:
1 int GCD (intint b)2{3 if(b = = 0)4 return A; 5 return GCD (b, a% b); 6 }
SUMMARY:GCD Greatest common divisor algorithm