Monday, 18 March 2013

Finding the Greatest Common Divisor


Finding the Greatest Common Divisor

The Euclidean algorithm is based on the following theorem: For any nonnegative integer a and any positive integer b,
Equation 4-4

gcd(55, 22) = gcd(22, 55 mod 22) = gcd(22, 11) = 11



[Page 108]
To see that Equation (4.4) works, let d = gcd(a, b). Then, by the definition of gcd, d|a and d|b. For any positive integer b, a can be expressed in the form
a = kb + r r (mod b)
a mod b = r


with k, r integers. Therefore, (a mod b) = a kb for some integer k. But because d|b, it also divides kb. We also have d|a. Therefore, d|(a mod b). This shows that d is a common divisor of b and (a mod b). Conversely, if d is a common divisor of b and (a mod b), then d|kb and thus d|[kb + (a mod b)], which is equivalent to d|a. Thus, the set of common divisors of a and b is equal to the set of common divisors of b and (a mod b). Therefore, the gcd of one pair is the same as the gcd of the other pair, proving the theorem.
Equation (4.4) can be used repetitively to determine the greatest common divisor.
gcd(18, 12) = gcd(12, 6) = gcd(6, 0) = 6
gcd(11, 10) = gcd(10, 1) = gcd(1, 0) = 1


The Euclidean algorithm makes repeated use of Equation (4.4) to determine the greatest common divisor, as follows. The algorithm assumes a > b > 0. It is acceptable to restrict the algorithm to positive integers because gcd(a, b) = gcd(|a|, |b|).
EUCLID(a, b)
1.   A  a; B  b
2.   if B = 0  return  A = gcd(a, b)
3.   R = A mod B
4.   A  B
5.   B  R
6.   goto 2



To find gcd(1970, 1066)
1970
= 1 x 1066 + 904
gcd(1066, 904)
1066
= 1 x 904 + 162
gcd(904, 162)
904
= 5 x 162 + 94
gcd(162, 94)
162
= 1 x 94 + 68
gcd(94, 68)
94
= 1 x 68 + 26
gcd(68, 26)
68
= 2 x 26 + 16
gcd(26, 16)
26
= 1 x 16 + 10
gcd(16, 10)
16
= 1 x 10 + 6
gcd(10, 6)
10
= 1 x 6 + 4
gcd(6, 4)
6
= 1 x 4 + 2
gcd(4, 2)
4
= 2 x 2 + 0
gcd(2, 0)
Therefore, gcd(1970, 1066) = 2


The alert reader may ask how we can be sure that this process terminates. That is, how can we be sure that at some point B divides A? If not, we would get an endless sequence of positive integers, each one strictly smaller than the one before, and this is clearly impossible.

No comments:

Post a Comment