Euclidean Algorithm gcd and its ultimate explanation

This problem has plagued me for a long time. I finally found an explanation, and I made some changes myself. I will certainly be able to deepen my understanding after my patience.

Extended Euclidean algorithms-solutions for indefinite equations, linear homogeneous equations.

When two frogs meet each other after step s, the following equations must be met:

(x+m*s)-(y+n*s)=k*l(k=0,1,2....)

Slightly changed:

(n-m)*s+k*l=x-y

N-m = a, k = B, x-y = c, that is

a*s+b*l=c

As long as the above formula has an integer solution, the two frogs can meet each other; otherwise, they cannot.

The first method is to use two for loops to enumerate the values of s and l to check whether there is an integer solution of s and l. If so, enter the smallest value of s,

But obviously this method is not feasible, and no one knows how big the smallest s is. If the smallest s is large, the timeout is obvious.

In fact, this problem can be quickly solved using the Euclidean extension principle. Let's first look at what is the Euclidean extension principle:

The euclidean algorithm, also known as the moving phase division, is used to calculate the maximum approximate number of two integers a and B. The computation principle depends on the following theorem:

Theorem: gcd (a, B) = gcd (B, a mod B)

Proof: a can be expressed as a = kb + r, then r = a mod B

Assume that d is a common divisor of a and B.

D | a, d | B, and r = a-kb, so d | r

Therefore, d is the common divisor of (B, a mod B ).

Assume that d is the common divisor of (B, a mod B ),

D | B, d | r, but a = kb + r

Therefore, d is also the common divisor of (a, B ).

Therefore, (a, B) and (B, a mod B) share the same common number, and the maximum common number must be equal.

The euclidean algorithm is based on this principle. Its algorithm is described in the C ++ language as follows:

int Gcd(int a, int b) { if(b == 0) return a; return Gcd(b, a % b); }

Of course, you can also write it as an iteration:

int Gcd(int a, int b) { while(b != 0) { int r = b; b = a % b; a = r; } return a; }

In essence, the above principle is used.

Supplement: The Extended Euclidean algorithm is used to solve a group of x in known a and B, and y makes a * x + B * y = Gcd (a, B) (the solution must exist, according to the related Theorem in number theory ). Extended Euclidean is often used in solving model Linear Equations and equations. Below is an example

Implementation with C ++:

int exGcd(int a, int b, int &x, int &y) { if(b == 0) { x = 1; y = 0; return a; } int r = exGcd(b, a % b, x, y); int t = x; x = y; y = t - a / b * y; return r; }

Comparing this implementation with the Recursive Implementation of Gcd, we found that the following x and y values are much greater, which is the essence of extending the Euclidean algorithm.

You can think like this:

For a' = B, B '= a % B, we obtain x and y to make a' x + B' y = Gcd (A', B ')

Because B '= a % B = a-a/B * B (Note: Here/is the division in the programming language)

Then we can get:

a'x + b'y = Gcd(a', b') ===> bx + (a - a / b * b)y = Gcd(a', b') = Gcd(a, b) ===> ay +b(x - a / b*y) = Gcd(a, b)

Therefore, for a and B, their corresponding p and q are respectively y and (x-a/B * y ).

Understanding of the Method for Solving x and y

Set a> B.

1. Obviously when B = 0, gcd (a, B) =. At this time, x = 1, y = 0;

2, AB <> 0

Set ax1 + by1 = gcd (a, B );

bx2+(a mod b)y2=gcd(b,a mod b);

According to the simple Euclidean principle, gcd (a, B) = gcd (B, a mod B );

Then: ax1 + by1 = bx2 + (a mod B) y2;

That is, ax1 + by1 = bx2 + (a-(a/B) * B) y2 = ay2 + bx2-(a/B) * by2;

According to the constant theorem, x1 = y2; y1 = x2-(a/B) * y2;

In this way, we obtain the Method for Solving x1 and y1: The values of x1 and y1 are based on x2 and y2.

The above idea is defined by recursion, because gcd's continuous recursive solution will always have B = 0, so recursion can

End.

I have read a lot of questions about the solution of the Indefinite Equation on the Internet, but I have not fully explained it. I have only talked about some of them. After reading a lot of questions, I can really figure out the whole process of the solution. The steps are as follows:

Evaluate the integer solution of a * x + B * y = c.

1. calculate Gcd (a, B) first. If n cannot be divisible by Gcd (a, B), the equation has no integer solution. Otherwise, the two sides of the equation are divided by Gcd (, b), obtain the new uncertainty equation a' * x + B '* y = C'. In this case, Gcd (A', B') = 1;

2. Use the Euclidean algorithm mentioned above to obtain a group of integers in equation a' * x + B '* y = 1 to solve x0 and y0, then c' * x0, c '* y0 is a group of Integer Solutions for equation a' * x + B' * y = C;

3. According to the related Theorem in number theory, All integers of the equation a' * x + B '* y = C' can be obtained and interpreted:

In fact, our solutions are just a group,

A * x0 + lcm (a, B) + B * y0-lcm (a, B) = 1;

A * x + B * y = 1;

X = x0 + B/gcd (a, B); y = y0-a/gcd (a, B );

A/gcd (a, B) * x' + B/gcd (a, B) * y' = c/gcd (a, B );

X' = c/gcd (a, B) * x0 + B/gcd (a, B); y' = c/gcd (a, B) * y0-a/gcd (a, B );

X = C' * x0 + B '* t y = C' * y0-a' * t (t is an integer)

The above solution is All Integer Solutions of a * x + B * y = n.