摘抄:欧几里得算法证明过程

GCD(A,B) = GCD(B, A mod B);
// A > B 且 R= A mod B, R 不等于 0

1.1 证明GCD(A,B) 能被 C 整除,C = A-B

A = X * GCD(A,B);
B = Y * GCD(A,B);

C = A - B = X * GCD(A,B) - Y * GCD(A,B);
C = (X - Y) * GCD(A,B);

1.2 证明GCD(B,C) 能被 A 整除

B = M * GCD(B,C);
C = N * GCD(B,C);

A = B + C = M * GCD(B,C) + N * GCD(B,C);
A = (M + N) * GCD(B,C);

1.3 证明GCD(A,B) = GCD(B, A-B)

C = X * GCD(A,B);
B = Y * GCD(A,B);
∴ GCD(A,B) <= GCD(B,C);

B = M * GCD(B,C);
A = N * GCD(B,C);
∴ GCD(B,C) <= GCD(A,B);

∴ GCD(A,B) = GCD(B,C);
GCD(A,B) = GCD(B, A-B);

1.4 证明GCD(A,B) = GCD(B,R)

GCD(A,B) = GCD(B,A-B)
         = GCD(B,A-2B)
         = GCD(B,A-3B) 
         = ...
         = GCD(B, A-QB);
         
A = B * Q + R (B-1 => R >= 0);
GCD(A,B) = GCD(B,R);

你可能感兴趣的:(算法)