Day 2-4 Maximum common factor and (extended) Euclidean algorithm

Source: Internet
Author: User
Tags gcd greatest common divisor

I. Introduction of Concepts

  GCD, full name greatest common divisor(max common factor).

We represent the maximum common factor of A and B with gcd (A, A, a) .

Two, Euclidean algorithm (aka Division method)Use:

Solution gcd (A, b)

Core formula:

    gcd (b) = gcd (b,a mod b) (where a mod b > 0)

Algorithm ideas:

(Guaranteed A>b)

When a is a multiple of B, a, A, B greatest common divisor A;

*ps: At this time, a mod b = 0, that is, gcd (A, b) = gcd (b,a mod) = gcd (b,0) = B, can be used for boundary judgment;

When a is not a multiple of B, use the core formula until a ' is a multiple of B '.

*ps: Because B ' = (a mod b) < b, so a, a, two values will gradually decrease in the process of calling GCD until b=0;

Code:
intgcdintAintb) {    if(b==0)        returnA; Else     {        if(a<b) {a=a+b;//a ' =a+bB=a-b;//B = A '-b = A+b-b = aA=a-b;//a ' = a '-b ' = A+b-a = b        }        returnGCD (b,a%b); }}
* Core Formula Certificate Method 1.

    The first step (Proof of D is the number of the conventions, D is also b,a MoD B's Convention number "reaction is going forward"):

--a can be expressed as A = kb + R (A,b,k,r are positive integers, and r<b),

--Then r = a mod b

-assuming D is a number of conventions for a, B, D|a, d|b, A and B can be divisible by D.

--and R = a-kb, both sides divided by d,r/d = A/D-kb/d = m, by the right of the equation (A and B can be divisible by D), I know M is an integer,

--so d|r, known as r = a mod b

--D|b and d| (a mod b)

-So d is also the number of B,a mod B conventions

    In the second step (proof that D is the B,a mod B convention number, D is also a, B Convention number "reverse reaction"):

-assuming D is the number of B,a mod b conventions, then d|b,d| (A-k*b), K is an integer,

--and then d|a.

-So d is also the number of conventions of a, B

-so the number of conventions (a, B) and (B,a) is the same, and their greatest common divisor are necessarily equal,

In conclusion, the core formula of Euclidean algorithm is proven.

Third, expand Euclidean algorithm  Use:

    The extended Euclidean algorithm is used to solve a set of X, Y, and B in a known

Make them satisfy the Bézout equation : ax+by = gcd (A, B) =d (the solution must exist, according to the correlation theorem in number theory).

It is commonly used in solving linear equations and equations of modules .

Programming EXGCD more to solve the " Chinese remainder theorem " related knowledge. For example, "If n is divided by more than 5 2, divided by 13 + 3, then n is the minimum, and what is the condition of all n?" ”

 Complexity of Time:

An O (N+LOGM) is used to calculate the greatest common divisor complexity of n a positive integer not exceeding m with the extended Euclidean algorithm.

Code:
/*Known as a, B (a>b), solves X, Y, satisfies ax+by=gcd (A, B), and returns GCD (A, b) at the same time*/intEXGCD (intAintBint&x,int&y)//x, Y uses the way reference is passed{    if(b==0) {x=1; Y=0; /*when a%b==0, it is obvious that gcd (A, A, A, b) =b, so B. = gcd (a) = GCD (b,a%b) = gcd (b,0) At this time obviously b*1+0*0 = gcd (b,0) = GCD ( A, B) So x=1,y=0*/    }    Else    {        //G=GCD (A, b)        intg = EXGCD (b,a%b,x,y); intt =x; X=y; Y= t-a/b *y; returnG; /*understanding of the method of solving X, y: Set a>b. 1, obviously when B=0,GCD (A, b) =a.      At this time x=1,y=0; 2,a>b>0 ax1+ by1= gcd (A, b);      bx2+ (a mod b) y2= gcd (b,a mod b);      According to the naïve Euclidean principle there is gcd (A, b) = gcd (b,a mod);      Then: ax1+ by1= bx2+ (a mod b) y2;      i.e.: 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;      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 GCD constantly recursive solution, there will be a time b=0,
So recursion can end. */ }}
Expand the Euclidean algorithm extension ~ (--n--*) ~ use:

The extended Euclidean algorithm not only calculates (a, b) the greatest common divisor, but also computes the multiplicative inverse (albeit not the best algorithm) of the A-and B-mode A to solve the "Chinese remainder theorem".

  /* I Caishuxueqian, this piece has not been seriously studied, later add it * *

Day 2-4 Maximum common factor and (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.