Extends Euclidean and Euclidean

Source: Internet
Author: User

Extends Euclidean and Euclidean

The euclidean algorithm, also known as the moving phase division, is used to calculate the maximum approximate number of two integers a and B.

Basic algorithm: Set a = qb + r, where a, B, q, and r are all integers, then gcd (a, B) = gcd (B, r ), that is, gcd (a, B) = gcd (B, a % B ).

Recursive code:

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

Extended Euclidean

Basic algorithm: for non-negative integers a, B, gcd (a, B) with an incomplete value of 0, it indicates the maximum approximate number of a and B,

There must be an integer pair x and y, making gcd (a, B) = ax +.

Proof: Set a> B.

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

2, AB! If it is 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 always has B = 0, so recursion can end.

Recursive code:

__int64 exgcd(__int64 a,__int64 b,__int64 &x1,__int64 &y1){    __int64 t,d;    if(b==0){        x1=1;        y1=0;        return a;    }    d=exgcd(b,a%b,x1,y1);    t=x1;    x1=y1;    y1=t-a/b*y1;    return d;}


The Application of Extended Euclidean algorithms mainly includes the following three aspects:

(1) solving an indefinite equation;

(2) solving model linear equations (linear homogeneous equations );

(3) solving the inverse element of the model;

Supplementary theorem:

1. Set a, B, and c to any integer. If a group of integers in the equation ax + by = c is interpreted as (x0, y0), any integer solution of it can be written as (x0 + kb ', y0-ka '), where a' = a/gcd (a, B), B '= B/gcd (a, B), k is any integer
2. theorem: If ax + by = g, (g = gcd (a, B), that is, g is the maximum approximate number of a and B) There is an integer solution; then ax + by = c (c is a multiple of g) has an integer solution



Extended Euclidean Algorithm PASCAL

Gcd (a, B) = ax +
Gcd (B, a mod B) = bx '+ (a mod B) y'
= Bx '+ (a-(a div B) * B) y'
= Bx '+ ay'-(a div B) * B * y'
= Ay '+ B (x'-(a div B) y ')
Because gcd (a, B) = gcd (B, a mod B)
So ax + by = ay '+ B (x'-(a div B) y ')
X = y'
Y = x'-(a div B) y'

Var
A, B, x, y, k: integer;

Function extended_gcd (a, B: longint; var x, y: longint): longint;
Var
T: longint;
Begin
If B = 0 then
Begin
X: = 1; y: = 0;
Exit ();
End;
Extended_gcd: = extended_gcd (B, a mod B, x, y );
T: = x;
X: = y;
Y: = t-(a div B) * y;
End;

Begin
Readln (a, B );
K: = extended_gcd (a, B, x, y );
Writeln (a, '* (', x, ') +', B, '* (', y, ') =', k );

Readln;
End.

The amount assigned is also less.

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>

Related Article

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.