Extended Euclidean algorithm (recursive and non-recursive c ++ version)

Today finally understood the extended Euclidean algorithm, have their own understanding, that is amazing to think of writing a blog.

 

Before introducing the extended Euclidean algorithm, we first recall the Euclidean algorithm.

Euclidean algorithm (Euclidean):

  Euclidean greatest common divisor, high school school, but at that time know not why, until college truly understand the essence of it.

  Dividing removed appreciated, the key is to be understood that gcd (a, b) == gcd (b, a% b)

  So how to understand it? Here is my understanding:

    For the first non-negative integers a, b, will be able to write a = k * b + form r (r <b) of

    So that g = gcd (a, b), there g | a, i.e., g | (k * b + r) 

            And g | b, so g | r

    Of course, only attained the same time divides b and g r still can not draw g == gcd (b, r), may also be gcd (b, r)> g.

    So we assume that there g '> g (so g' can not be a divisor of a, b) of

            Such that g '| b and g' | r

            There g '| (k * b + r) i.e., g' | a

           → g 'is a, b divisor of the contradictions.

    Therefore, gcd (a, b) == gcd (b, r) (where r == a% b) is proved.

 

  The following recursive and non-recursive code is posted Euclidean algorithm:

1  // recursive Euclidean algorithm 
2  int GCD ( int A, int B)
 . 3  {
 . 4      return B == 0 A: GCD (B, A%? B);
 . 5 }
1  // nonrecursive Euclidean algorithm 
2  int GCD ( int A, int B)
 . 3  {
 . 4      int T;
 . 5      the while (B) {
 . 6          T = B;
 . 7          B = A% B;
 . 8          A = T;
 . 9      }
 10      return A;
 . 11 }

  It is worth noting:

    1, the greatest common divisor any positive integer and is a positive integer of 0 itself.

    2, even if the parameter a <b, or after a recursive loop, a, b exchanged automatically, no need to add it at the beginning if (a <b) swap (a, b); statement.

 

Extended Euclidean algorithm:

  In fact, the extended Euclidean algorithm to determine ax + by = an integer solution gcd (a, b) of the (x 0 , Y 0 ), all of the integer solutions of (x and able to come to 0 + K * B, Y 0 -k * A).

  Recursive

     Recursive extended Euclidean algorithm is based on recursive evaluation of gcd process, when it completes the recursive function returns the gcd stepwise, an integer solution (x, y).

     Let's look at a simple simulation process gcd (a, b) recursive return (recurrence formula derived by mathematical induction)

       ① In the deepest recursive gcd (touches first layer) when, i.e., case B 0 == 0, the equation can be derived A 0 X + B 0 Y = gcd (A, B) (gcd (A, B) GCD == (a 0 , B 0 ) == GCD (a . 1 , B . 1 ) ... == a == 0 ) solution is a set of integer X 0 =. 1, Y 0 = 0.

       ② When the equation is assumed to return to the penultimate layer k A k X B + k Y = GCD (A, B) the solution of a set of integers X k , Y k 

        And provided k + 1 is returned to the first layer a reciprocal equation k + 1 X + B k + 1 Y = GCD (a, B) the solution of a set of integers X k + 1  , Y k + 1 

        There are A K X K + B K Y K = A K +. 1 X K +. 1 + B K. 1 + Y K +. 1  wherein A K == B K +. 1 , B K = A K +. 1 % B K + 1 

        Might make X K +. 1 = Y K    Y K +. 1 = X K -a K +. 1 / B K +. 1 * Y K 

     Recursive coding posted below:

1  // recursive Extended Euclidean Algorithm 
2  int exgcd ( int A, int B, int & X, int & Y)
 . 3  {
 . 4      IF (B == 0 ) {
 . 5          X = 1 , Y = 0 ;
 . 6          return A ;
 . 7      }
 . 8      int R & lt exgcd = (B, A% B, Y, X);
 . 9      Y- = A / B * X;
 10      return R & lt;
 . 11 }

  Non-recursive

    Personal feeling extended to Europe a few non-recursive algorithm Reid way easier to understand.

    We start from a concrete example of it.

        If a = 30, b = 47, then the gcd (a, b) = 1, then the integer to be solved is 30x + 47y = 1 solution of indeterminate equation.

        From known, we have

                     

 

           Observation of the matrix, the elements of the third column transformation process happens Euclidean greatest common divisor of the division process.

        Their transform of each step (row conversion) represented by the following general formula:

               

 

        When the second element row 3 is 0, the first row of three elements is the gcd (a, b).

        Therefore, the final solution can be obtained a set of integers (x, y) = (11, -7) satisfies 30x + 47y = 1.

        So we can write non-recursive extended Euclidean algorithm on the basis of non-Euclidean recursive algorithm, the code is as follows:

1  // nonrecursive Extended Euclidean Algorithm 
2  int exgcd ( int A, int B, int & X, int & Y)
 . 3  {
 . 4      int m = 0 , = n- 1 , T;
 . 5      X = 1 , Y = 0 ;
 . 6      the while (B) {
 . 7          T = m, m = XA / B * m, X = T;
 . 8          T = n-, n-= Ya / B * n-, Y = T;
 . 9          T = B, B = A% B, A = T;
 10      }
 . 11      return A;
 12 is }

 

I write to be finally finished.

This is the first blog I wrote, originally aimed at the core of the idea with the most simple language to describe, but it probably looks like ...... ...... ...... may not match expectations ... with a little

I hope that by writing blog record your own ideas, even if you ever forget can see at a glance. Of course, if you can help others understand the depth, that's fine.

Finally, if you find that the text of the error, have any suggestions or have some of their own ideas are welcome in the comments area.

Guess you like

Origin www.cnblogs.com/zbhfz/p/11267438.html