Greatest common divisor (GCD) algorithm (Euclid)

Source: Internet
Author: User
Tags gcd greatest common divisor

One article reproduced from the three fist of the farmer

Euclidean algorithm and extended Euclidean algorithm

Euclidean algorithm, also known as the greatest common divisor method, is used to calculate two integers, a, b, and so on. Its computational principle relies on the following theorem:

Theorem: gcd (b) = gcd (b,a mod b)

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

Euclidean algorithm is based on this principle, its algorithm is described in C + + language as:

int gcd (intint  b) {    if0return  A;     return gcd (b,a% b);}

Of course you can also write this form:

int gcd (intint  b) {    while0)    {        int t = b;         = a% b;         = t;    }     return A;}

Supplement: The extended Euclidean algorithm is used to solve a set of p,q in known a, b to make a * p+b * q = gcd (A, B) (the solution must exist, according to the correlation theorem in number theory). Extended Euclidean is commonly used in solving linear equations and equations. The following is an implementation that uses C + +:

intEXGCD (intAintBint&x,int&y) {    if(b = =0) {x=1; Y=0; returnA; }    intR = EXGCD (b,a%b,x,y); intt =x; X=y; Y= t-a/b *y; returnR;}

Comparing this implementation with the recursive implementation of GCD, we find that the following X, Y assignment process is found, which is the essence of extending Euclid's algorithm.

Can think like this:

For a ' = B, b ' = a% B, we get x, Y makes a ' x + b ' y = Gcd (a ', B ')

Because b ' = a% b = a-a/b * B (Note: This is a division in the programming language)

Then you 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)

So for A and B, their relative p,q are Y and (x-a/b*y) respectively.

Greatest common divisor (GCD) algorithm (Euclid)

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