Extended Euclidean algorithm, Reid Algorithm

Source: Internet
Author: User

Extended Euclidean algorithm, Reid Algorithm

The original mathematics was not good. When I saw the LRJ mathematical topic, I went online to Baidu to learn how to prove the Extended Euclidean algorithm.

First, let's talk about the simple Euclidean algorithm, that is, the division of the moving phase, which is very simple.

int gcd(int a,int b){    return b == 0 ? a : gcd(b,a % b);}

The following describes how to extend the Euclidean algorithm.

Returns x and y FOR a and B so that a * x + B * y = gcd (a, B );

Let's set a> B, and x> y

Then when B = 0, gcd (a, B) = a, so x = 1, y = 0;

If B! When the value is 0, we know that

Ax1 + by1 = gcd (a, B) = bx2 + (a % B) y2; (according to the simple Euclidean)

Let's simplify this formula.

Ax1 + by1 = bx2 + (a-(a/B) * B) y2;

Ax1 + by1 = bx2 + a * y2-(a/B) * B * y2;

Ax1 + by1 = a * y2 + B * (x2-(a/B) * y2 );

Therefore, x1 = y2;

Y1 = (x2-(a/B) * y2 );

That is to say, the x and y of each layer can be obtained from the x and y of the previous layer, and the recursive endpoint is B = 0;

# Include <cstdio> # include <cstring> # include <iostream> # include <algorithm> # include <vector> # include <stack> # include <queue> # include <map> # include <set> # include <list> # include <string> # include <cmath> # include <sstream> # include <ctime> using namespace std; # define _ PI acos (-1.0) # define INF 1 <10 # define esp 1e-6typedef long LL; typedef unsigned long ULL; typedef pair <int, int> pill; /* ====================================== ========================================================== ===================================== */Void gcd (int, int B, int & d, int & x, int & y) {if (! B) {d = a; x = 1; y = 0;} else {gcd (B, a % B, d, y, x ); y-= x * (a/B) ;}} int main () {int x, y, a, B, c; while (scanf ("% d ", & a, & B )! = EOF) {x = max (a, B); y = x; if (a <B)/* guarantees a> B */swap (a, B ); gcd (a, B, c, x, y);/* represents a * x + B * y = c */printf ("(% d * % d) + (% d * % d) = % d \ n ", a, x, B, y, c);} return 0 ;}


Ps: In addition, if multiple groups of solutions are required, assume that a group of solutions (x0, y0) has been obtained, and set: a' = a/gcd (a, B ), B '= B/gcd (a, B), then any other solutions can be written as (x0 + k * B', y0-k * ');


Better understanding of Extended Euclidean Algorithms

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,
Mod B)
Proof: a can be expressed as a = kb +
R, r = a mod B
Assume that d is a common divisor of a and B.
D |,
D | B, and r = a-kb, so d | r
Therefore, d is (B,
Mod B)
Assume that d is (B,
Mod B), then
D | B, d
| R, but a = kb + r
Therefore, d is also the common divisor of (a, B ).
So (a, B) and (B, a mod
B) the maximum number of common appointments must be the same.
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;
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,
B solves a group of x and y so that 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;
}
Int r =
ExGcd (B, a % B, x, y );
Int t =
X;
X =
Y;
Y = t-
/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 '=
% 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
Y and (x-a/B * y ).
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:
Calculate a * x
+ Integer solution of B * y = n.
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) to obtain the new INDEFINITE EQUATION'
* X + B '* y = n'. In this case, Gcd (A', B') = 1;
2. Use the Euclidean algorithm mentioned above to find a group of integers in the equation a' * x + B '* y = 1 to solve x0 and y0, then n' * x0, n '*
Y0 is a group of Integer Solutions for equation a' * x + B '* y = n'... the rest of the full text>

Better understanding of Extended Euclidean Algorithms

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,
Mod B)
Proof: a can be expressed as a = kb +
R, r = a mod B
Assume that d is a common divisor of a and B.
D |,
D | B, and r = a-kb, so d | r
Therefore, d is (B,
Mod B)
Assume that d is (B,
Mod B), then
D | B, d
| R, but a = kb + r
Therefore, d is also the common divisor of (a, B ).
So (a, B) and (B, a mod
B) the maximum number of common appointments must be the same.
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;
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,
B solves a group of x and y so that 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;
}
Int r =
ExGcd (B, a % B, x, y );
Int t =
X;
X =
Y;
Y = t-
/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 '=
% 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
Y and (x-a/B * y ).
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:
Calculate a * x
+ Integer solution of B * y = n.
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) to obtain the new INDEFINITE EQUATION'
* X + B '* y = n'. In this case, Gcd (A', B') = 1;
2. Use the Euclidean algorithm mentioned above to find a group of integers in the equation a' * x + B '* y = 1 to solve x0 and y0, then n' * x0, n '*
Y0 is a group of Integer Solutions for equation a' * x + B '* y = n'... the rest of the full text>

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.