Euclidean Algorithm (also called phase Division)
Theorem: gcd (a, B) = gcd (a, a mod B)
Proof: For any positive integers A and B. If A> B, both have a = K * B + R, that is, r = A-K * B => r = a mod B.
Assume that D is the common divisor of A and B, then a = A1 * D, B = b1 * D.
R = A1 * D-K * B1 * D = (a1-k * B1) * D => D is also the approximate number of R => D is also the common number of (A, R)
It indicates the common divisor of (a, B), that is, the common divisor of (A, R. Therefore, gcd (a, B) = gcd (a, a mod B ).
Code
/*** Calculate the maximum common approx. ** the Euclidean algorithm is also called the moving phase division. It is used to calculate the maximum common approx. Of two integers A and B. * The calculation principle depends on the following theorem: gcd (a, B) = gcd (B, A mod B) **/public class eucliddivisor {public static int getdivisor (int, int B) {if (a % B = 0) return B; If (B % A = 0) return a; return a> = B? Getdivisor (a, a % B): getdivisor (A, B % A);} public static void main (string [] ARGs) {system. out. println (eucliddivisor. getdivisor (12, 8 ));}}I am the dividing line of tiantiao