Its calculation principle relies on the following theorem: Theorem: gcd (A, a, b) = gcd (b,a MoD) (A>b and a mod B is not 0) proves that: A can be expressed as A = kb + R, then r = a mod b assumes that D is a number of conventions of a A, then d|a,d|b, and R = A-KB, so d|r D is therefore also (B,a mod b) The number of conventions so (b) and (b,a mod b) The number of conventions is the same, and its greatest common divisor is necessarily equal, provable or: proof: First step: Make C=GCD (A, b), then set a=mc,b= NC Second step: According to the premise of the R =a-kb=mc-knc= (M-KN) C third step: According to the second step results that C is the factor of R Fourth step: can determine m-kn and n reciprocity "otherwise, can be set M-kn=xd,n=yd, (d>1), then m=kn+xd=kyd+ Xd= (ky+x) d, then a=mc= (ky+x) DC,B=NC=YCD, so A and B greatest common divisor ≥cd, rather than C, with the previous conclusion contradiction "so that gcd (B,r) =c, and then gcd (A, A, A, b) =gcd (B,R), the two methods are the same in essence.
#include<stdio.h>
int GCD (int M,int N)
{
int temp;
while (N > 0) {
temp = M% N;
M = N;
N = temp;
}
return M
}
int Main ()
{
int A, b;
scanf ("%d%d", &a,&b);
printf ("%d",Gcd(A, b));
return 0;
}
Euclidean algorithm, greatest common divisor