~ Office Supplies ~~ Buy Posters ~~ A-Z Products ~~ Website Advertising


Euclidean algorithm - Wikipedia

<<Up     Contents

Euclidean algorithm

Redirected from Euclidean Algorithm

The Euclidean algorithm (also called Euclid's algorithm) is an extremely efficient algorithm to determine the greatest common divisor (GCD) of two integers. It is described in Euclid's Elements. The algorithm does not require factoring.

Given two natural numbers a and b, first check if b is zero. If yes, then the GCD is a. If no, calculate c, the remainder after the division of a by b. Replace a with b, b with c, and start the process again.

For example, the GCD of 1071 and 1029 is computed by this algorithm to be 21 with the following steps:

a       b       c

1071102942
10294221
42210
210

The algorithm can be formulated in the Python programming language as follows:

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

(The absolute value is used in the last line to ensure that the algorithm correctly deals with negative inputs; e.g. gcd(-7,0) = 7.)

By keeping track of the quotients occurring during the algorithm, one can also determine integers p and q with ap+bq = gcd(a,b). This is known as the extended Euclidean algorithm.

These algorithms can be used in any context where division with remainder is possible. This includes rings of polynomials over a field as well as the ring of Gaussian integers, and in general all Euclidean domains.

Euclid originally formulated the problem geometrically, as the problem of finding a common "measure" for two line lengths, and his algorithm proceeded by repeated subtraction of the shorter from the longer segment. This is illustrated with the following implementation in Python, which works only for positive inputs and is considerably less efficient than the method explained above:

 def gcd(a,b):
    while a != b:
        if a > b:
            a = a - b
        else:
            b = b - a
    return a

Proof of the correctness of the Euclidean Algorithm

The proof of this algorithm is not difficult. Suppose a and b are the numbers whose GCD has to be determined. And suppose the remainder of the division of a by b is c. Therefore a = qb + c where q is the quotient of the division. Now any common divisor of a and b also divides c (since c can be written as c = a - qb); similarly, any common divisor of b and c will also divide a. Thus the greatest common divisor of a and b is the same as the greatest common divisor of b and c. Therefore it is enough if we continue the process with the numbers b and c. Since c is smaller in absolute value than b, we will reach c = 0 after finitely many steps.

Runtime

When analyzing the runtime of Euclid's algorithm, it turns out that the inputs requiring the most steps are two successive Fibonacci numbers, and the worst case requires Θ(n) divisions, where n is the number of digits in the input (see Big O notation).

Continued fractions

The quotients that appear when the Euclidean algorithm is applied to the inputs a and b are precisely the numbers occurring in the continued fraction representation of a/b. Take for instance the example of a = 1071 and b = 1029 used above. Here is the calculation with highlighted quotients:

1071 = 1029 × 1 + 42
1029 = 42 × 24 + 21
42 = 21 × 2 + 0
From this, one can read off that
<math>\frac{1071}{1029} = \mathbf{1} + \frac{1}{\mathbf{24} + \frac{1}{\mathbf{2}}}</math>.
This method can even be used for real inputs a and b; if a/b is irrational, then the Euclidean algorithm won't terminate, but the computed sequence of quotients still represents the (now infinite) continued fraction representation of a/b.

wikipedia.org dumped 2003-03-17 with terodump




 
 
158 carats gray AGATE gem Polished slab rectangle block Cabbing cab cabochon rough gemstone 31 grams
 158 carats gray AGATE Polished slab rectangle block Cabbing cab cabochon 31 grams 
 
30 gram picture MOONSTONE feldspar orthoclase gem Cab cabochon raw rough jewelry gemstone 154 carat
 30 gram picture MOONSTONE feldspar orthoclase Cab cabochon raw jewelry 154 carat 
 
15 carats yellow Oregon SUNSTONE gem stones Semi Facet cabbing rough gemstones lot Faceting jewels
 15 carats yellow Oregon SUNSTONE Semi cabbing lot ing jewels 
 
Red green AMMOLITE gem stone Freeform cabochon cabbing jewelry rough Ammonite opal 10 carats 2 grams
 Red green AMMOLITE Freeform cabochon cabbing jewelry Ammonite opal 10 carats 2 grams 
 
22 carat green new jade Serpentine rough gemstone tumble polished nugget drilled necklace bead Nice
 22 carat green new jade Serpentine tumble polished nugget drilled necklace bead Nice