Turn from online Daniel Blog, speak of easy to understand.
Original address: http://www.cnblogs.com/frog112111/archive/2012/08/19/2646012.html
Euclidean algorithm
Euclidean algorithm, also known as the greatest common divisor method, is used to calculate two integers, a, b, and so on.
Basic algorithm: Set A=qb+r, where a,b,q,r are integers, then gcd (A, B) =gcd (b,r), gcd (A, B) =gcd (b,a%b).
The first kind of 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
The second kind of proof:
To be certified Euclidean algorithm is established, namely: GCD (A, B) =gcd (b,r), where GCD is to take greatest common divisor meaning, r=a mod b
The following certificate gcd (A, B) =gcd (B,R)
Set C is the greatest common divisor of a, B, C=GCD (A, B), there is A=MC,B=NC, where m,n is a positive integer, and m,n are prime numbers
by r= a mod B, r= a-qb which, q is a positive integer,
Then r=a-qb=mc-qnc= (M-QN) c
B=nc,r= (M-QN) C, and N, (M-QN) coprime (assuming n,m-qn not coprime, N=xd, m-qn=yd where x,y,d are positive integers, and d>1
Then a=mc= (qx+y) DC, B=XDC, at this time a B greatest common divisor into DC, with the premise contradiction,
So n, m-qn must coprime)
Then GCD (b,r) =C=GCD (A, B)
Evidence.
Extended Euclidean algorithm
Basic algorithm: For a non-negative integer A,B,GCD (A, B) that is not exactly 0, the greatest common divisor of A/b is bound to have an integer pair of x, Y, which makes gcd (A, b) =ax+by.
Proof: Set A>b.
1, obviously when B=0,GCD (A, b) =a. At this time x=1,y=0;
When 2,ab!=0
Set AX1+BY1=GCD (A, b);
bx2+ (a mod b) y2=gcd (b,a mod b);
According to the naïve Euclid principle there is gcd (A, B) =gcd (b,a MoD);
Then: ax1+by1=bx2+ (a mod b) y2;
namely: ax1+by1=bx2+ (A-(A/b) *b) y2=ay2+bx2-(A/b) *by2;
According to the identity theorem: x1=y2; Y1=x2-(A/b) *y2;
This gives us a way to solve x1,y1: The value of X1,y1 is based on X2,y2.
The idea above is defined recursively, because the recursive solution of GCD will always have a time b=0, so recursion can end.
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;
(1) using the extended Euclidean algorithm to solve the indefinite equation:
for indefinite integer equation pa+qb=c, if C mod Gcd (p, q) = 0, then the equation has an integer solution, otherwise there is no integer solution.
A method of finding an integer solution is listed above, and after finding a set of solutions for P * A+q * b = GCD (p, Q) p0,q0, the other integer solutions of p * A+q * b = GCD (p, Q) are satisfied:
p = p0 + B/GCD (p, q) * t
Q = Q0-A/GCD (p, q) * t (where T is an arbitrary integer)
As for the integer solution of Pa+qb=c, it is only possible to multiply each solution of P * A+q * b = GCD (P, Q) by C/GCD (p, q).
After finding a set of solutions for P * A+q * b = GCD (A, B) p0,q0, you should be given a set of solutions for P * A+q * b = c P1 = p0* (C/GCD (A, b)), Q1 = q0* (C/GCD (b)),
P * A+q * b = Other integer solutions of C are satisfied:
p = p1 + B/GCD (A, b) * t q = Q1-A/GCD (A, b) * t (where T is an arbitrary integer) p, Q is the solution of all integers of p * a+q * b = c. Relevant proofs can be consulted: http://www.cnblogs.com/void/archive/2011/04/18/2020357.html
(2) The method of solving the modal linear equation with the extended Euclidean algorithm:
The congruence equation Ax≡b (mod n) has a solution for unknown x, when and only if GCD (a,n) | B. And when the equation has a solution, the equation has a gcd (a,n) solution.
Solving equation ax≡b (mod n) is equivalent to solving equation ax+ ny= B, (x, y is an integer)
Set d= gcd (a,n), if integers x and y, satisfy D= ax+ ny (with extended Euclidean). If d| b, the equation
A * x0+ n y0= D, the equation is multiplied by b/d, (because d|b, so divisible), get a A * x0* b/d+ n y0* b/d= B.
So x= x0* b/d,y= y0* b/d a solution for ax+ ny= B, so x= x0* b/d for ax= B (mod n).
A solution for ax≡b (mod n) is x0= x* (b/d) mod n, and the equation's D solution is xi= (x0+ i* (n/d)) mod n {i= 0 ... d-1}.
Set ans=x* (b/d), s=n/d;
The minimum integer solution for equation ax≡b (mod n) is: (ans%s+s)%s;
Relevant proof:
The proof equation has one solution: x0 = X ' (b/d) mod n;
by a*x0 = A*x ' (b/d) (mod n)
a*x0 = d (b/d) (mod n) (due to ax ' = d (mod n))
= B (mod n)
The proof equation has a D solution: Xi = x0 + i* (n/d) (mod n);
by A*xi (mod n) = A * (x0 + i* (n/d)) (mod n)
= (a*x0+a*i* (n/d)) (mod n)
= A * x0 (mod n) (due to D | a)
= b
Let's look at a simple example:
5x=4 (MOD3)
Solution x = 2,5,8,11,14 ....
Thus a rule is found, that is, the interval of the solution is 3.
So how is the interval of the solution determined?
If the first solution can be found and the interval between the solutions can be obtained, then the solution set of the linear equation of the modulo is obtained.
We set the interval between the solutions to DX.
So there are
A*x = B (mod n);
A * (X+DX) = B (mod n);
Two-type subtraction, get:
A*DX (mod n) = 0;
That is to say, A*DX is a multiple, but also a multiple of n, that is, A*DX is a and n common multiple. In order to find the DX, we should find a and n least common multiple, at this time the corresponding DX is the smallest.
The greatest common divisor for A and n are D, so the least common multiple of A and n is (a*n)/d.
That is A*DX = a*n/d;
So dx = n/d.
So the interval between the solutions is calculated.
(3) using Euclidean algorithm to find the inverse of the model:
The congruence equation Ax≡b (mod n), if gcd (a,n) = = 1, the equation has only the unique solution.
In this case, if b== 1, the congruence equation is ax=1 (mod n), gcd (a,n) = 1.
At this point the X is calculated as the inverse of the modulo n multiplication of a.
For the congruence Equation ax= 1 (mod n), the solution of gcd (a,n) = 1 is to solve the equation
ax+ ny= 1,x, y is an integer. This can be extended Euclidean algorithm to find out that the only solution to the original congruence equation is to expand the Euclidean algorithm to derive the X.
Euclid and the extended Euclidean algorithm