Euclidean algorithm, also known as the common factor method, is used to calculate the maximum of two nonnegative integers. Its pseudo-code is as follows:
GCD (A, B)//required to ensure incoming a>=b
if (b = = 0)
Return a
return gcd (b, a% b)
First, it shows that this function can return the maximum common factor of A and B. But we don't go from code to principle, we're going to return the code from the principle. (all occurrences of the following symbols are non-negative integers)
In cases where A and B are not 0 and a>=b, if C is the largest common factor (c>0) of A and B, then C|a and C|b are established. Obviously A=i*c,b=j*c, here should meet 1<=j<=i. Kee I=k*j+r, where 0<=r<j, then I%j=r,floor (i/j) = K. and the corresponding a=i*c= (k*j+r) *c=k*j*c+r*c=k*b+r*c, here r*c<j*c=b, so there is floor (A/b) = k and a% B = R * C. To this we found an interesting phenomenon, a%b and B have common factor C.
Again, C must be the largest common factor of a%b and B. Assuming the existence of d>c, so that D is a%b and B of the public factor, then there is a%b=q*d,b=w*d. Then a=e*b+a%b= (e*w+q) *d, obviously D also is a and b of the common factor, which is contrary to the premise that C is the largest common factor of a and B, so the assumption is not established, so all a%b and B of the public factor will not exceed C, so C is a%b and b the largest common factor.
Here we can conclude that when both A and B are not 0 and a>=b, the maximum common factor of A and B is exactly the same as the maximum common factor of a%b and B.
Whether the feeling is gradually clear. If both A and B are not 0 and a>=b, then the maximum common factor C of a and B can be obtained by calculating the maximum common factor of a%b and B. If the same a%b is not 0, then the b% (a%b) and the a%b of the common factor can be calculated in disguise to obtain a and B of the maximum common factor C. Until a parameter is 0, notice that the maximum common factor of any positive integer x and 0 is x, which simply returns X. This is exactly the same as the code we GCD.
After analyzing the correctness of the algorithm, it is necessary to further analyze the time complexity and space complexity of the program. The spatial complexity of O (1) should be questioned because no additional space is allocated during the recursive process. The following is a description of time complexity:
Notice that the time spent on each GCD call (not counting the recursive part) is constant, so the time complexity of calling a GCD is O (1). And how many times has the gcd been called in the calculation of a maximum public factor? Notice that in the process of recursive invocation, its passed in parameter is b,a%b. Among them b<=a, while A%b<=min (A-b,b) <=a/2. So we find that each time gcd is called, an incoming parameter value is not changed, and the value of the other passed in parameter is reduced by at least half. By the definition of GCD, when any one of the parameters is reduced to 0 o'clock recursion will end, so in the process of seeking the maximum common factor for a, a, a maximum of log2 (a) +log2 (b) gcd, where log2 (a) order a halved, log2 (b) Order B halved. Therefore, the total time complexity is O (1) * (log2 (a) +log2 (b)) =o (LOG2 (Max (a)).
Euclid's algorithm can be used not only to derive the maximum common factor of a and B, but also to find such a value I, which makes
That is, I is the minimum natural number that can satisfy the conditional i*a=j*b, where j is also the natural number.
How to do that, in fact, C is the largest common factor of a and B, then x*a=y*b can write x* (A/C) =y* (b/c) equivalently, because C is the largest common factor, so A/C with b/c maximum common factor is 1, that is, both coprime. By the arithmetic basic theorem can be derived (A/C) |y and (b/c) |x divisible. And since x is a positive integer, the possible value of X is (B/C), 2 (B/C), 3 (B/3),...。 The interesting thing is (B/C) * (A/c) = (A/C) * (B/C), that is, x= (b/c), y= (A/C) can get the formula Xa=yb satisfied, and the number of times x value is exactly the smallest possible value, so i=x=b/c. The Euclidean algorithm, however, provides a highly efficient method of calculating C, so I=B/GCD (A, B).
In the case of known C, the space-time complexity of calculation I is O (1).
In fact here the possible value of all x can also find another interesting property, which satisfies the function of x*a=y*b, if x is a positive integer, then x must be a multiple of i= (B/C).
Again, one of Euclid's uses is to calculate a possible value for coefficients x and y in Xa+yb=c, where C is the maximum common factor of A and B (A and B cannot be 0 at the same time).
Suppose A>=b. If b=0, then obviously C=a, and at this time 1*a+0*b=c is a very perfect solution.
Now that you have found the values for Q and W in q*b+w* (a%b) =c, you can launch c=q*b+w* (a%b) =q*b+w* (A-floor (A/b) *b) =w*a+ (Q+w*floor (A/b)) *b, x=w,y= (q+w* Floor (A/b), the formula Xa+yb=c is met.
And what does this have to do with Euclid's algorithm? Of course! We call the last recursion in the Euclidean algorithm (that is, the recursive operation with a condition that has a parameter of 0) called Condition 1, and the corresponding call condition 1 that recursion is referred to as Condition 2, and the call condition 2 is called case 3, so as to continue, The first call (that is, the one with the parameters A and B) is bound to be considered a condition N. By with the condition 1 has been guaranteed to find X and y so that the equation is established, and for any case k>=1, if the condition K can find the corresponding coefficients so that the parameters AK,BK meet Xk*ak+yk*bk=c, you can find Xk+1*ak+1+yk+1*bk+1=c, that is, the situation K + 1 can also find the corresponding parameters to set up the formula. Using the mathematical induction method, we know that for any case I (I is a natural number), the corresponding coefficients can be found so that the formula is established. So naturally we can also find the coefficients x and y that make xa+yb=c.
Coe (A, b)//a to ensure greater than B
if (b = = 0)
Return (1, 0)
r = Coe (b, b% a)
Return (R.Y, r.x + r.y * Floor (A/b))
The time complexity of the Coe, like GCD, can be obtained using the same analysis method.
If you want to find a non-negative and minimal X-value in all of these coefficients, you can combine the scenarios in the previous section. First find
Next make x plus or minus (depending on the current x symbol) I, minus or plus J at the same time. At this time c=c+0= (x+i) A + (Y-J) b= (x-i) A + (y+j) b. This cycle keeps x close to 0 until the i>x>=0 happens. This is to show that the x that can be obtained at this time is indeed the smallest nonnegative integer. If there is a smaller nonnegative integer m and the corresponding n makes ma+nb=c, then there is (XA+YB)-(MA+NB) = (x-m) *a+ (y-n) b=c-c=0, and here is the definition of i>x>=x-m, which is in accordance with the formula of this type (ia+jb=0) The definition of the minimum positive integer is inconsistent, so the hypothesis is not true, so the resulting x is the smallest non-negative integer of the formula (XA+YB=C). Multiplication and division can be used here in one step, so the space-time complexity of this operation is O (1) in the case of any given set of X and Y.
MinX (A, B, X, y)
if (x < 0)
Return (x% i + I, Y-(Floor (x/i) + 1) * j)
Return (x I, Y-floor (x/i) * j)
Euclidean algorithm (greatest common divisor algorithm) complete analysis