Extended Euclidean algorithm

Source: Internet
Author: User
Tags gcd greatest common divisor

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.

The implementation of the algorithm:

The simplest method is to apply a recursive algorithm, the code is as follows:

View Code
1 int gcd (int a,int b) 2 {3     if (b==0) 4         return a;5     return 6         gcd (b,a%b); 7}

The code can be optimized as follows:

View Code
1 int gcd (int a,int b) 2 {3     return B? gcd (b,a%b): A;4}

Of course, you can also use an iterative form:

View Code
1 int Gcd (int a, int b) 2 {3 while     (b! = 0) 4     {5     int r = B; 6     B = a% B; 7     a = R; 8     } 9
   return a;10}

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.

Extended Euclid's Recursive code:

View Code
1 int exgcd (int a,int b,int &x,int &y) 2 {3     if (b==0) 4     {5         x=1; 6         y=0; 7         return A; 8     } 9< C8/>int R=EXGCD (b,a%b,x,y);     int t=x;11     x=y;12     y=t-a/b*y;13     return r;14}

Extended Euclidean non-recursive code:

View Code
1 int exgcd (int m,int n,int &x,int &y) 2 {3     int x1,y1,x0,y0; 4     x0=1; y0=0; 5     x1=0; y1=1; 6     x=0; y = 1; 7     int r=m%n; 8     int q= (m-r)/n; 9     while (R)     {one         x=x0-q*x1; y=y0-q*y1;12         x0=x1; y0=y1;13         X1=x; Y1=y;14         m=n; n=r; r=m%n;15         q= (m-r)/n;16     }17     return n;18}

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. The relevant proofs can be consulted: http://www.cnblogs.com/void/archive/2011/04/18/2020357.html the indefinite equation ax+by=c with the extended Euclidean algorithm; The code is as follows:View Code
1 bool linear_equation (int a,int b,int c,int &x,int &y) 2 {3     int d=exgcd (a,b,x,y); 4     if (c%d) 5         return false;6     int k=c/d;7     x*=k; y*=k;    Only one set of solutions is obtained 8     return true;9}

(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

     proving 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.

The code is as follows:

View Code
1 bool modular_linear_equation (int a,int b,int N) 2 {3     int x,y,x0,i; 4     int d=exgcd (a,n,x,y); 5     if (b%d) 6         return false; 7     x0=x* (b/d)%n;   Special Solution 8     for (i=1;i<d;i++) 9         printf ("%d\n", (x0+i* (n/d))%n);     return true;11}

(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.

Extended Euclidean algorithm

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.