FazBrowse Wikipedia Viewer | Main Page |
Search Wikipedia:
|
Viewing: https://en.wikipedia.org/wiki/Euclidean_algorithm

Euclidean algorithm

Contents
    Visualization
    Implementations
Mathematical applications
    Matrix method

Euclid's method for finding the greatest common divisor (GCD) of two starting lengths BA and DC, both defined to be multiples of a common "unit" length. The length DC being shorter, it is used to "measure" BA, but only once because the remainder EA is less than DC. EA now measures (twice) the shorter length DC, with remainder FC shorter than EA. Then FC measures (three times) length EA. Because there is no remainder, the process ends with FC being the GCD. On the right Nicomachus's example with numbers 49 and 21 resulting in their GCD of 7 (derived from Heath 1908:300).

In mathematics, the Euclidean algorithm, or Euclid's algorithm, is an efficient method for computing the greatest common divisor (GCD) of two integers, the largest number that divides them both without a remainder. It is named after the ancient Greek mathematician Euclid, who first described it in his Elements (105 (as 21 is also the GCD of 21 = 5 × 105 + (−2) × 252). The fact that the GCD can always be expressed in this way is known as Bézout's identity.

The version of the Euclidean algorithm described above—which follows Euclid's original presentation—may require many subtraction steps to find the GCD when one of the given numbers is much bigger than the other. A more efficient version of the algorithm shortcuts these steps, instead replacing the larger of the two numbers by its remainder when divided by the smaller of the two (with this version, the algorithm stops when reaching a zero remainder). With this improvement, the algorithm never requires more steps than five times the number of digits (base 10) of the smaller integer. This was proven by Gabriel Lamé in 1844 (Lamé's Theorem), and marks the beginning of computational complexity theory. Additional methods for improving the algorithm's efficiency were developed in the 20th century.

The Euclidean algorithm has many theoretical and practical applications. It is used for reducing fractions to their simplest form and for performing division in modular arithmetic. Computations using this algorithm form part of the cryptographic protocols that are used to secure internet communications, and in methods for breaking these cryptosystems by factoring large composite numbers. The Euclidean algorithm may be used to solve Diophantine equations, such as finding numbers that satisfy multiple congruences according to the Chinese remainder theorem, to construct continued fractions, and to find accurate rational approximations to real numbers. Finally, it can be used as a basic tool for proving theorems in number theory such as Lagrange's four-square theorem and the uniqueness of prime factorizations.

The original algorithm was described only for natural numbers and geometric lengths (real numbers), but the algorithm was generalized in the 19th century to other types of numbers, such as Gaussian integers and polynomials of one variable. This led to modern abstract algebraic notions such as Euclidean domains.

The Euclidean algorithm calculates the greatest common divisor (GCD) of two natural numbers g is the largest natural number that divides both gcd(a, b) or, more simply, as a and b are themselves prime numbers. For example, 6 = 2 × 3 and 35 are coprime, with no common factors other than c only if b.

Let b are both multiples of b = ng, and there is no larger number ''g''"}},"i":0}}]}'/>G > g for which this is true. The natural numbers m and c that divides both g. The greatest common divisor b is the unique (positive) common divisor of c.

The greatest common divisor can be visualized as follows. Consider a rectangular area c that divides both c, which divides the rectangle into a grid of squares of side length c for which this is possible. For illustration, a 2×2 squares, 6×6 squares or 24 and 12×12 squares, with two squares along one edge (a and b. For example, since 3213 can be factored into 3213 equals 1 (obtained here as an instance of the empty product); in other words, they are coprime. A key advantage of the Euclidean algorithm is that it can find the GCD efficiently without having to compute the prime factors. Factorization of large integers is believed to be a computationally very difficult problem, and the security of many widely used cryptographic protocols is based upon its infeasibility.

Another definition of the GCD is helpful in advanced mathematics, particularly ring theory. The greatest common divisor b is also their smallest positive integral linear combination, that is, the smallest positive number of the form v are integers. The set of all integral linear combinations of g (a and a and q0 = ; r0 = GCD( , ) = .r1 = 0 the algorithm is finished. Thus = q2 × + r2

Since


q3 = ; r3 = GCD( , ) = .r4 = 0 the algorithm is finished. Thus = q5 × + r5

Since
q6 = ; r6 =
GCD( , ) =
.r7 = 0 the algorithm is finished. Thus = q8 × + r8

Since
q9 = ; r9 =
GCD( , ) =
.r10 = 0 the algorithm is finished. Thus Restart r−2=a and rk+1<rk. The integer rk via division-with-remainder on the preceding pair rk−2=qk⋅rk−1+rk, with  rk−1>rk≥0.

Because the sequence of non-negative integers k, and each nth step with r0, we need to find integers 1071=q0⋅462+r0.

This is the quotient r0=147 and so the sequence is now q1 and q1=3 since {1071, 462, 147, r1=21}. The next step is to continue the sequence to find r2<r1 such that:

147=7⋅21+0. This determines 0 can be found. The penultimate remainder a and gcd(a, a)=a as the sequence of remainders will be a≡0⋅b+a, suggesting the next remainder should be r0<r−1 but now we have rk<rk−1 only for k≥0 is strictly decreasing, therefore excluding rN=0 follows from the condition rN−1 being equal to gcd⁡(x,0)=x, for all 0 are exactly all the divisors of gcd⁡(x,y)=gcd⁡(y,x−zy)

In fact, the sets of common divisors of the pairs d is a common divisor of X,Y such that x−zy=dX−zdY=d(X−zY), where x−zy.
Conversely, if y, then there are integers x−zy=dZ and Z+zY is an integer. Therefore, y.

Now, the Euclidean algorithm starts with the pair (rk−1,rk−2−qkrk−1)=(rk−1,rk). Property 2 shows that the (a,b) and the last pair a = 1071 and 462×147 rectangle. This rectangle is tiled with 21×21 squares, leaving no uncovered area. The smallest square size, 462.

For illustration, the Euclidean algorithm can be used to find the greatest common divisor of 462 are subtracted from q0 = 2), leaving a remainder of 147 are subtracted from q1 = 3), leaving a remainder of 21 are subtracted from q2 = 7), leaving no remainder:

1071 and 1071 = q0 462 + r00</sub> = 2"}},"i":0}}]}'/>q0 = 2 and 0</sub> = 147"}},"i":0}}]}'/>r0 = 147 11</sub> 147 + ''r''<sub>1</sub>"}},"i":0}}]}'/>462 = q1 147 + r11</sub> = 3"}},"i":0}}]}'/>q1 = 3 and 1</sub> = 21"}},"i":0}}]}'/>r1 = 21 22</sub> 21 + ''r''<sub>2</sub>"}},"i":0}}]}'/>147 = q2 21 + r22</sub> = 7"}},"i":0}}]}'/>q2 = 7 and 2</sub> = 0"}},"i":0}}]}'/>r2 = 0; algorithm ends

Visualization

edit

The Euclidean algorithm can be visualized in terms of the tiling analogy given above for the greatest common divisor. Assume that we wish to cover an b×b square tiles; however, this leaves an 0</sub>×''b''"}},"i":0}}]}'/>r0×b residual rectangle untiled, where 0</sub> < ''b''"}},"i":0}}]}'/>r0 < b. We then attempt to tile the residual rectangle with 0</sub>×''r''<sub>0</sub>"}},"i":0}}]}'/>r0×r0 square tiles. This leaves a second residual rectangle 1</sub>×''r''<sub>0</sub>"}},"i":0}}]}'/>r1×r0, which we attempt to tile using 1</sub>×''r''<sub>1</sub>"}},"i":0}}]}'/>r1×r1 square tiles, and so on. The sequence ends when there is no residual rectangle, i.e., when the square tiles cover the previous residual rectangle exactly. The length of the sides of the smallest square tile is the GCD of the dimensions of the original rectangle. For example, the smallest square tile in the adjacent figure is 1071 and

At every step rk from two numbers ''k''−1</sub>"}},"i":0}}]}'/>rk−1 and ''k''−2</sub>"}},"i":0}}]}'/>rk−2

''k''−2</sub> = ''q''<sub>''k''</sub> ''r''<sub>''k''−1</sub> + ''r''<sub>''k''</sub>"}},"i":0}}]}'/>rk−2 = qk rk−1 + rk,

where the ''k''</sub>"}},"i":0}}]}'/>rk is non-negative and is strictly less than the absolute value of ''k''−1</sub>"}},"i":0}}]}'/>rk−1. The theorem which underlies the definition of the Euclidean division ensures that such a quotient and remainder always exist and are unique.

In Euclid's original version of the algorithm, the quotient and remainder are found by repeated subtraction; that is, ''k''−1</sub>"}},"i":0}}]}'/>rk−1 is subtracted from ''k''−2</sub>"}},"i":0}}]}'/>rk−2 repeatedly until the remainder ''k''</sub>"}},"i":0}}]}'/>rk is smaller than ''k''−1</sub>"}},"i":0}}]}'/>rk−1. After that ''k''</sub>"}},"i":0}}]}'/>rk and ''k''−1</sub>"}},"i":0}}]}'/>rk−1 are exchanged and the process is iterated. Euclidean division reduces all the steps between two exchanges into a single step, which is thus more efficient. Moreover, the quotients are not needed, thus one may replace Euclidean division by the modulo operation, which gives only the remainder. Thus the iteration of the Euclidean algorithm becomes simply

''k''</sub> = ''r''<sub>''k''−2</sub> mod ''r''<sub>''k''−1</sub>"}},"i":0}}]}'/>rk = rk−2 mod rk−1.

Implementations

edit

Implementations of the algorithm may be expressed in pseudocode. For example, the division-based version may be programmed as

function gcd(a, b) while b ≠ 0 t := b b := a mod b a := t return a

At the beginning of the rk−1, whereas the variable b := a mod b is equivalent to the above recursion formula ''k''</sub> ≡ ''r''<sub>''k''−2</sub> mod ''r''<sub>''k''−1</sub>"}},"i":0}}]}'/>rk ≡ rk−2 mod rk−1. The temporary variable rk is being calculated. At the end of the loop iteration, the variable a holds its predecessor, ''k''−1</sub>"}},"i":0}}]}'/>rk−1.

(If negative inputs are allowed, or if the mod function may return negative values, the last line must be replaced with '''return abs'''(a)</code>"}},"i":0}}]}'>return abs(a).)

In the subtraction-based version, which was Euclid's original version, the remainder calculation (b := a '''mod''' b</code>"}},"i":0}}]}'>b := a mod b) is replaced by repeated subtraction. Contrary to the division-based version, which works with arbitrary integers as input, the subtraction-based version supposes that the input consists of positive integers and stops when b alternate holding the previous remainders ''k''−1</sub>"}},"i":0}}]}'/>rk−1 and ''k''−2</sub>"}},"i":0}}]}'/>rk−2. Assume that a equals ''k''−2</sub>"}},"i":0}}]}'/>rk−2, since ''k''−2</sub> > ''r''<sub>''k''−1</sub>"}},"i":0}}]}'/>rk−2 > rk−1. During the loop iteration, a is smaller than rk. Then a, giving the next remainder ''k''+1</sub>"}},"i":0}}]}'/>rk+1, and so on.

The recursive version is based on the equality of the GCDs of successive remainders and the stopping condition ''N''−1</sub>, 0) = ''r''<sub>''N''−1</sub>"}},"i":0}}]}'/>gcd(rN−1, 0) = rN−1.

function gcd(a, b) if b = 0 return a else return gcd(b, a mod b)

(As above, if negative inputs are allowed, or if the mod function may return negative values, the instruction '''return''' a</code>"}},"i":0}}]}'>return a must be replaced by '''return max'''(a, −a)</code>"}},"i":0}}]}'>return max(a, −a).)

For illustration, the gcd(147, 462 mod 147) = gcd(147, 21), which in turn is calculated from the |rk−1| > rk > 0. However, an alternative negative remainder ''k''</sub>"}},"i":0}}]}'/>ek can be computed:

''k''−2</sub> = (''q''<sub>''k''</sub> + 1) ''r''<sub>''k''−1</sub> + ''e''<sub>''k''</sub>"}},"i":0}}]}'/>rk−2 = (qk + 1) rk−1 + ek

if ''k''−1</sub>&nbsp;>&nbsp;0"}},"i":0}}]}'/>rk−1 > 0 or

''k''−2</sub> = (''q''<sub>''k''</sub> – 1) ''r''<sub>''k''−1</sub> + ''e''<sub>''k''</sub>"}},"i":0}}]}'/>rk−2 = (qk – 1) rk−1 + ek

if ''k''−1</sub>&nbsp;<&nbsp;0"}},"i":0}}]}'/>rk−1 < 0.

If ''k''</sub>"}},"i":0}}]}'/>rk is replaced by ''k''</sub>"}},"i":0}}]}'/>ek. when ''k''</sub>}} < {{abs|''r''<sub>''k''</sub>}}"}},"i":0}}]}'/>|ek| < |rk|, then one gets a variant of Euclidean algorithm such that

''k''</sub>}} ≤ {{abs|''r''<sub>''k''−1</sub>}} / 2"}},"i":0}}]}'/>|rk| ≤ |rk−1| / 2

at each step.

Leopold Kronecker has shown that this version requires the fewest steps of any version of Euclid's algorithm. More generally, it has been proven that, for every input numbers a and b, the number of steps is minimal if and only if ''k''</sub>"}},"i":0}}]}'/>qk is chosen in order that g that measures a and m multiples of the smaller pile from the larger. Thus, if the two piles consist of x is larger than x − my stones, as long as the latter is a nonnegative integer. The winner is the first player to reduce one pile to zero stones.

Mathematical applications

edit
Bézout's identityedit
b can be represented as a linear sum of the original two numbers s and s and q1, etc. by reversing the order of equations in Euclid's algorithm. Beginning with the next-to-last equation, rN−2 and ''N''−3</sub>"}},"i":0}}]}'/>rN−3:

rN−2 = rN−4 − qN−2 rN−3 and
''N''−3</sub> = ''r''<sub>''N''−5</sub> − ''q''<sub>''N''−3</sub> ''r''<sub>''N''−4</sub>"}},"i":0}}]}'/>rN−3 = rN−5 − qN−3 rN−4.

Substituting these formulae for ''N''−2</sub>"}},"i":0}}]}'/>rN−2 and ''N''−3</sub>"}},"i":0}}]}'/>rN−3 into the first equation yields rN−5. The process of substituting remainders by formulae involving their predecessors can be continued until the original numbers r2 = r0 − q2 r1

1</sub> = ''b'' − ''q''<sub>1</sub> ''r''<sub>0</sub>"}},"i":0}}]}'/>r1 = b − q1 r0
0</sub> = ''a'' − ''q''<sub>0</sub> ''b''"}},"i":0}}]}'/>r0 = a − q0 b.

After all the remainders 0</sub>"}},"i":0}}]}'/>r0, 1</sub>"}},"i":0}}]}'/>r1, etc. have been substituted, the final equation expresses b, so that a and u and b are both divisible by g. This is true for every common divisor of g. A smaller common divisor cannot be a member of the set, since every member of the set must be divisible by g can be obtained by choosing s and ua + vb is equivalent to the set of multiples a and a and b. By adding/subtracting ua + vb can be measured out. These volumes are all multiples of

The integers sk = sk−2 − qksk−1

''k''</sub> = ''t''<sub>''k''−2</sub> − ''q''<sub>''k''</sub>''t''<sub>''k''−1</sub>"}},"i":0}}]}'/>tk = tk−2 − qktk−1

with the starting values

−2</sub> = 1, ''t''<sub>−2</sub> = 0"}},"i":0}}]}'/>s−2 = 1, t−2 = 0
−1</sub> = 0, ''t''<sub>−1</sub> = 1"}},"i":0}}]}'/>s−1 = 0, t−1 = 1.

Using this recursion, Bézout's integers s = sN and ''N''</sub>"}},"i":0}}]}'/>t = tN, where k − 1 of the algorithm; in other words, assume that

''j''</sub> = ''s''<sub>''j''</sub> ''a'' + ''t''<sub>''j''</sub> ''b''"}},"i":0}}]}'/>rj = sj a + tj b

for all kth step of the algorithm gives the equation

''k''</sub> = ''r''<sub>''k''−2</sub> − ''q''<sub>''k''</sub>''r''<sub>''k''−1</sub>"}},"i":0}}]}'/>rk = rk−2 − qkrk−1.

Since the recursion formula has been assumed to be correct for ''k''−2</sub>"}},"i":0}}]}'/>rk−2 and ''k''−1</sub>"}},"i":0}}]}'/>rk−1, they may be expressed in terms of the corresponding ''k''</sub> = (''s''<sub>''k''−2</sub> ''a'' + ''t''<sub>''k''−2</sub> ''b'') − ''q''<sub>''k''</sub>(''s''<sub>''k''−1</sub> ''a'' + ''t''<sub>''k''−1</sub> ''b'')"}},"i":0}}]}"/>rk = (sk−2 a + tk−2 b) − qk(sk−1 a + tk−1 b).

Rearranging this equation yields the recursion formula for step rk = sk a + tk b = (sk−2 − qksk−1) a + (tk−2 − qktk−1) b.

Matrix method

edit

The integers


View original on Wikipedia | Back | FazBrowse Home