Euclidean algorithm and its expansion

To be perfect storm to die

1. Euclidean algorithm

Euclidean algorithm known as Euclidean, means for calculating two positive integers a, b is the greatest common divisor . There are two applications of mathematics and computer. Calculated gcd (a, b) = gcd (b, a mod b);

Euclidean algorithm can be used to expand RSA encryption and other fields.

Step Euclidean algorithm is as follows, assuming that we require 762 and 230 of the greatest common divisor of two positive integers, then the following process:

  • I 72 762/230 = 3
  • I 14 230/72 = 3
  • 72/14 = 4 5 I
  • I 214/4 = 3
  • 4/2 = 2 0 I

Divisor and remainder division repeatedly done, when the remainder is 0, the current formula greatest common divisor is also desired, so that the greatest common divisor of 762 and 230 2, which is implemented as a python code:

def gcd(a, b):
    while a % b != 0:
        a, b = b, a % b
    return b

However, considering the computer division difficult, the greatest common divisor programming method should be better subtraction operation using these computers easier to implement, as follows:

def gcd(a, b):
    while a > 

2. expand the Euclidean algorithm

Extended Euclidean Algorithm (English: ExtendedEuclideanalgorithm) is the Euclidean algorithm (also known as Euclidean) expansion. Known integers a, b, Extended Euclidean Algorithm can be obtained at the same time a, b of the greatest common divisor, to find an integer x, y (which is likely to be a negative number), Bezu that they satisfy the equation:

ax + by = gcd(a, b)

If a is negative, the problem can be converted into

|a|(-x) + by = gcd(|a|, b)

Then allowed to x '= (- x).

There are two numbers a, b, Euclidean them, their greatest common divisor available - as is well known. Then, the collection was removed division algorithm formula generated, go back, integer solutions can be obtained ax + gcd (a, b) is by =.
Extended Euclidean algorithm can be used to calculate the modular multiplicative inverse (also called inverse mode), while the modular multiplicative inverse plays a key role in the RSA encryption algorithm.

Guess you like

Origin www.cnblogs.com/chuaner/p/11536491.html