Euclidean algorithm and extended Euclidean algorithm

Source: Internet
Author: User
Tags gcd greatest common divisor

Euclidean algorithm is what we often say the method of greatest common divisor, the division method can be used to seek the greatest common divisor, know that the least common multiple can also be begged. GCD seems to have a library function __gcd

int GCD (intint  b) {    while0)    {      int r = b;     = a% b;     = r;    }     return A;}
non-recursive method
int gcd (int A,int  b) {return b? gcd (B,A%B): A;}
Recursive method

The extended Euclidean algorithm is solving a problem, solving the equation, and solving the general solution of a two-yuan equation.

The extended Euclidean algorithm is an extension of Euclid's algorithm (also called the Euclidean method). In addition to calculating the greatest common divisor of a and b two integers, the algorithm can also find integers x, y (one of which is probably a negative number). When it comes to the maximum common factor, we all mention a very basic fact: given two integers a and B, there must be integers x and y to make ax + by = gcd (A, B). There are two numbers, A/b, which can be greatest common divisor by dividing them-which is well known. Then, by collecting the formulas produced in the AX+BY=GCD, we can get the integer solution of the (A, B). by Baidu Encyclopedia

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;}
non-recursive method
intEXGCD (intMintNint&x,int&y) {    intx1,y1,x0,y0; X0=1; y0=0; X1=0; y1=1; X=0; y=1; intr=m%N; intq= (M-R)/N;  while(r) {x=x0-q*x1; y=y0-q*Y1; X0=x1; y0=Y1; X1=x; y1=y; M=n; N=r; r=m%N; Q= (m-r)/N; }    returnN;}
Recursive method

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;

Euclidean algorithm 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.