Extended Euclidean algorithm-Extended Euclidean Algorithm

Source: Internet
Author: User

The euclidean algorithm, also known as the moving phase division, is used to calculate the maximum approximate number of two integers A and B. The computation principle depends on the following theorem: The GCD function is used to calculate the maximum approximate number of (a, B. Basic properties of the gcd function: gcd (a, B) = gcd (B, A) = gcd (-a, B) = gcd (| A |, | B |) the formula expression gcd (a, B) = gcd (B, A mod B) proves that a can be expressed as a = kb + R, then R = a mod B Assumes that D is, d | a, d | B, r = A-kb, so d | r is (B, A mod B) assume that D is (B, A mod B), d | B, d | r, but a = kb + R, so d is (a, B) so (A, B) and (B, A mod B) are the same, and the maximum number of them must be equal.

Extended:

Expressed in mathematical language: Calculate the integer x and y to make AX + by = C. it can be found that if gcd (a, B) | C is not true, that is, C is not a multiple of the maximum common divisor of A and B, there is obviously no integer solution.

Therefore, we generally calculate AX + by = gcd (a, B), as long as we obtain an integer solution (x, y), then any C (except gcd (A, B )) you can get it by multiplying the two sides by a multiple.

Code:

#include <iostream>using namespace std;int ext_gcd(int a,int b,int &x,int &y){if(b==0){x=1;y=0;return a;}int d=ext_gcd(b,a%b,y,x);y=y-(a/b)*x;//  x=_y;return d;//  y=_x-(a/b)*_y;} int main(){int x,y,a=99,b=78;cout<<ext_gcd(a,b,x,y)<<endl;cout<<x<<" "<<y<<endl;}

Note:

Assume that we have obtained:

_ D = gcd (B, A mod B) and _ d = B * _ x + (a mod B) * _ y (_ d, _ x, _ y) for recursive procedures, there are

D = gcd (a, B) = _ d = gcd (B, A mod B), in order to obtain the X and Y that meet d = AX +, use the preceding equation d = _ d for Deformation:

D = B * _ x + (a-(A/B) * _ y = a * _ y + B (_ x-(A/B) * _ y)

When x = _ y and Y = _ x-(A/B) * _ y are selected, the equation can be satisfied. In this way, recursion can proceed. Of course, the conditions for recursion termination are: B = 0, return ()

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.