Euclidean algorithm is what we often say the method of greatest common divisor, the division method can be used to seek the greatest common divisor, know that the least common multiple can also be begged. GCD seems to have a library function __gcd
int GCD (intint b) { while0) { int r = b; = a% b; = r; } return A;}
non-recursive method
int gcd (int A,int b) {return b? gcd (B,A%B): A;}
Recursive method
The extended Euclidean algorithm is solving a problem, solving the equation, and solving the general solution of a two-yuan equation.
The extended Euclidean algorithm is an extension of Euclid's algorithm (also called the Euclidean method). In addition to calculating the greatest common divisor of a and b two integers, the algorithm can also find integers x, y (one of which is probably a negative number). When it comes to the maximum common factor, we all mention a very basic fact: given two integers a and B, there must be integers x and y to make ax + by = gcd (A, B). There are two numbers, A/b, which can be greatest common divisor by dividing them-which is well known. Then, by collecting the formulas produced in the AX+BY=GCD, we can get the integer solution of the (A, B). by Baidu Encyclopedia
intEXGCD (intAintBint&x,int&y) { if(b==0) {x=1; Y=0; returnA; } intR=EXGCD (b,a%b,x,y); intt=x; X=y; Y=t-a/b*y; returnR;}
non-recursive method
intEXGCD (intMintNint&x,int&y) { intx1,y1,x0,y0; X0=1; y0=0; X1=0; y1=1; X=0; y=1; intr=m%N; intq= (M-R)/N; while(r) {x=x0-q*x1; y=y0-q*Y1; X0=x1; y0=Y1; X1=x; y1=y; M=n; N=r; r=m%N; Q= (m-r)/N; } returnN;}
Recursive method
The application of the extended Euclidean algorithm mainly has the following three aspects:
(1) solving indefinite equation;
(2) solving the linear linear equation (congruence);
(3) The inverse element of the solution module;
Euclidean algorithm and extended Euclidean algorithm