Euclidean algorithm and extended Euclidean algorithm

Source: Internet
Author: User
Tags gcd

Euclid's algorithm is based on such a GCD recursive theorem:

$GCD (A, b) = gcd (b, a\bmod{b}) $

The proof is as follows:

Suppose $a > b$, $a = kb + R (0 <= R < b) $, i.e. $a \bmod{b} = R $.

If there is $d \mid a$ and $d \mid b$, there must be $d \mid a-kb$, that is, $d \mid R. It is learned that the number of $a $ with $b $ of all conventions must be $b $ with $r $ of the number of conventions.

If there is $d \mid R $ and $d \mid b$, there must be $d \mid KB + R $, which is $d \mid a$. It is learned that the number of $b $ with $r $ of all conventions must be $a $ with $b $ of the number of conventions.

So $GCD (A, b) = gcd (b, r) $, i.e. $GCD (A, b) = gcd (b, a\bmod{b}) $

The $GCD (A, b) $ can then be calculated by continually returning to the $b = 0$.

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

The extended Euclidean algorithm utilizes some useful information from Euclid's algorithm to find a set of solutions with the Comodule equation $ax + by = gcd (A, b) $. The mathematical induction method can prove the correctness of the extended Euclidean algorithm.

1. When $b = 0$, $GCD (A, b) = a$, obviously $x = 1, y = 0$ is a set of valid solutions

2. When $b > 0$, suppose we have obtained the equation $bx + (a\bmod{b}) y = gcd (b, a\bmod{b}) $ for a set of solutions $ (x_0, y_0$), while setting $ (x_1, Y_1) $ is the equation $ax + by = GCD (A, b) $ of a set of solutions, then there are

$ax _1 + by_1 = bx_0 + (a\bmod{b}) y_0$

$ax _1 + by_1 = bx_1 + (A-\frac{a}{b}\cdot b) y_1$

$ax _1 + by_1 = B (X_0-\frac{a}{b}\cdot y_0) + ay_0$

correspond to equal, we can get

$x _1 = y_0$

$y _1 = x_0-\frac{a}{b}\cdot y_0$

void exgcd (intint int int. int int &y)    {if  (! b) {        = A;         1 ;         0 ;     Else {        % B, D, y, x);         -= (A/b) * x;    }}

(The code used a little bit of a trick: let $x $ with $y $ in recursive calls to reduce the amount of code, collation of the corresponding relationship can be found correct.)

Using Euclidean algorithm to get equation $ax + by = gcd (A, b) $ for a set of solutions $ (x_1, y_1) $ after assuming another set of equations for the solution of $ (x_2, y_2) $, then there are

$ax _1 + by_1 = ax_2 + by_2$

Organized to

$a (x_1-x_2) = B (y_2-y_1) $

Divide the two sides $g =GCD (A, b) $, get

$a ' (x_1-x_2) = B ' (y_2-y_1) $

Because $a ' $ with $b ' $ coprime, so $b ' \mid (x_1-x_2) $, you can set $x _1-x_2 = KB ' $, then there are

$a ' k = y_2-y_1$

That is $y _2 = Y_1+ka ' $

At the same time also got $x _2 = x_1+kb ' $

From the top, get a set of solutions $ (x_1, y_1) $ after you get another set of solutions $x _2 = X_1+kb ', y_2 = Y_1+ka ' $.

By extending Euclidean algorithm, we can solve the general indefinite equation $ax + by = C $ solution:

If $\GCD (A, B) \mid C $, the equation has a solution. Assuming $ (x_0, Y_0) $ is the equation $ax + by = gcd (A, b) $ for a set of solutions, then we can get the equation $ax + by = C $ for a set of solutions $ (\FRAC{C}{GCD (A, B)}\cdot{x_0}, \FRAC{C}{GCD (A, B)} \CDOT{Y_0}) $;

Otherwise, the equation has no integer solution.

In addition, we can use the extended Euclidean algorithm to solve the linear same comodule equation set. Like what:

$x \equiv m_1\pmod{a_1}$

$x \equiv m_2\pmod{a_2}$

...

$x \equiv m_n\pmod{a_n}$

We can merge it 22. For example, for the first two equations, suppose $m _1y+a_1=x$, $m _2z+a_2=x$, then

$m _1y-m_2z=a_2-a_1$

This is a general indefinite equation, using the extended Euclidean algorithm to get a $y _0$ value, we can calculate a feasible $x _0$ value, then obviously the general solution of X is

$x = X_0 + t[m_1, m_2]$

By this equation we can construct a new equation:

$x \equiv x_0\pmod{[m_1, m_2]}$

The equation is then coupled with the third equation to get a new equation. Repeat this process to get a solution to the equation set.

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.