Euclidean veritas, extended Euclidean and modular linear equations.

Source: Internet
Author: User
Tags greatest common divisor

Euclidean algorithm: A fast algorithm for the greatest common divisor of two integers. That is what is commonly referred to as the "Euclidean method". Given two integers a, b. Euclid can find A, B greatest common divisor in the complexity of log (max (|a|, |b|)) in the worst possible degree. The computational method of time complexity is also very interesting, see "Introduction to Algorithms".

Prove the correctness of Euclid's algorithm:

A can be expressed as A = kb + R, and r = a mod b

We're going to prove the correctness of Euclid's algorithm.
That is to prove gcd (A, b) = gcd (b, A%b=r)

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 (A, a) and (B,a mod b) The number of conventions is the same, and its greatest common divisor is necessarily equal.

Euclid's Certificate!!!

Code:

int gcd (intint  b) {    return b? gcd (b, a%B): A;}

Extended Euclidean algorithm: The extended Euclidean algorithm is used to solve a set of X, Y, in known a, b to satisfy the Bézout equation: ax+by = gcd (A, B) =d (the solution must exist, according to the correlation theorem in number theory). Extended Euclidean is commonly used in solving linear equations and equations.

Proof: (Interpretation of X and Y)

1, obviously when B=0,GCD (A, b) =a. At this time x=1,y=0;2,ab<>0 ax1+ by1= gcd (A, B), bx2+ (a mod) y2= gcd (b,a mod b); According to the simple Euclidean principle there is gcd (A, A, a, b) = gcd (B,a mod B.); x1+ by1= bx2+ (a mod b) y2; that is: ax1+ by1= bx2+ (A-[A/b] * b) y2=ay2+ bx2-[A/b] * BY2; that is ax1+ by1 = ay2+ B (x2-[A/b] * y2); According to the identity theorem: x1=y2; y1=x2-[A/b] *y2; so we get a way to solve x1,y1: The value of X1,y1 is based on x2,y2. The idea above is defined recursively, because GCD constant recursive solution will have a time b=0, so recursion can end. Code:
void extend_gcd (intint int int. int int &y)    {  If10;}     Else {EXTEND_GCD (b, a%b, D, y, x); y-= x* (A/b);}}

Extended Euclidean application: Mainly used to solve the indefinite equation, the solution of linear equations (i.e.: linear with congruence), in order to find the inverse of the element.

ways to solve indefinite equations using the extended Euclidean algorithm:

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 all integer solutions of p * A+q * b = c.

(2) Solving the mode linear equation:

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 proof 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

Of course, the proofs of these theorems are in the introduction to algorithms, but the introduction is based on rigorous mathematical logic and language reasoning, precision and elegance, but unfortunately, not suitable for the poor mathematical basis of reading.

(3) Inverse, obviously, in ax-ny = B, when B = 1 o'clock. Ax = 1 (mod n). At this point: the solution (x), called A on the inverse of modulo n. Similar to the concept of "reciprocal" in real arithmetic.

Equation: Ax-ny = 1. There is a solution, and it is clear that 1 must be a multiple of gcd (A, N) (extended Euclidean). So a, n-biotin. So: Ax = 1 (mod n) has only the unique solution.

PS: The solution of the congruence equation refers to an equivalence class.

Precipitation, to Be continued ~ ~ ~ ~.

Euclidean veritas, extended Euclidean and modular linear equations.

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.