The Euclidean Algorithm The Euclidean Algorithm appears in Book VII in Euclid’s The Elements, written around 300 BC. It is one of the oldest mathematical algorithms. It is also one of the most applicable. The algorithm provides a systematic way to find the greatest common divisor GCD of two integers and provide additional important information about the relationship between the GCD and the two integers involved. Modern technology uses a variety of algorithms based on modular arithmetic including the public-key encryption RSA algorithm. Many of these algorithms in turn rely on the Euclidean Algorithm as an algorithm acting on the ring of integers or as an algorithm acting on a ring of polynomials. Here we introduce the Euclidean algorithm for the integers. The Euclidean Algorithm on the set of polynomials is similar. The concepts here may be generalized to any algebraic system which obeys the division algorithm; such rings are called Euclidean Domains. 1.1 Introduction We say that the integer d divides the integer a (written d|a) if there is an integer k such that a = dk. For example, −5|20 since 20 = (−5)(−4). So the divisors of 20 are −20, −10, −5, −4, −2, −1, 1, 2, 4, 5, 10, 20. Given two integers a and b, we seek divisors d which divide both of these integers. We are in particular interested in the largest such divisor, the greatest common divisor of both a and b. (The set of all common divisors of a and b is exactly the set of divisors of the greatest common divisor.) Hereafter we abbreviate “greatest common divisor” of a and b by GCD or GCD(a, b). In order to avoid issue about ”size”, we will define the GCD of integers a and b as the positive integer d that satisfies the following condition: If c|a and c|b then c|d (1) If a and b have common divisors (other than −1, 1) then there is a common prime p dividing both of them. The GCD of a and b is 1 if and only if the only common divisors of a and b are −1 and 1. In this case (since −1 and 1 divide every integer) we say that a and b “have no common divisors.” Equivalently we say a and b are relatively prime. If an integer d divides both integers a and b then for any integer q, it divides a − qb. In particular, if d is the greatest divisor of a and b and r is the remainder (guaranteed by the division algorithm) upon division of a by q, then d also divides r. Conversely, if d divides b and d divides r = a−qb then d also divides a. Therefore the greatest common divisor of a and b is also the greatest common divisor of b and r. This is the essence of the Euclidean algorithm. We replace a pair of integes a and b by a smaller pair of integers b and r and iterate the process until we reach the smallest possible pair of integers. Suppose we are given two integers, a and b (for example, a = 843 52256 45419 and b = 105 46961 61403). Instead of hunting for divisors of a (8435225645419), we may divide the smaller number b (1054696161403) into the larger, and use the division algorithm to get a remainder r = a − qb, where 0 ≤ r < b. (In this case q = 7 and the remainder is r = 23436 45805.) Now we want the GCD of b and r, which are smaller numbers. Since the size of the positive integers is dropping, we may repeat this step, replacing a pair of integers ai and bi by bi and ri = ai − qbi 1 until we finally get a remainder of zero. Since, in the last step, the GCD of zero and an integer bk is just bk (every integer divides zero!), then the final integer bk is the GCD of a and b. This algorithm carries more information than might be obvious at first glance. Suppose we write a = a(1) + b(0), and b = a(0) + b(1), and, at each stage, write the new number in the form as + bt for integers s and t. Since each step in our algorithm involves computing ai −qbi , we may think of this process as an elementary row operation on a matrix of integers with rows of the form bi = as + bt, si , ti . (See Wikipedia for more on matrices and elementary row operations.) We will work out our example with a = 8435225645419 and b = 1054696161403 in detail. Table 1, below, is the algorithm in tabular form. The four columns represent −q (where q is the quotient in the current step of the division algorithm), bi = as + bt, s, and t. In the last two rows, we compute the GCD of 12347 and 0, and so the GCD of the original two numbers must be 12347. The process of computing the GCD of a = 8435225645419 and b = 1054696161403 requires nine rows. Table 1: Euclidean algorithm in tabular form −q -7 -1 -449 -42 -5 -4 -214 as + bt a = 8435225645419 b = 1054696161403 1052352515598 2343645805 55549153 10581379 2642258 12347 0 s 1 0 1 -1 450 -18901 94955 -398721 85421249 t 0 1 -7 8 -3599 151166 -759429 3188882 -683180177 Notice that this algorithm always allows us to write the GCD of a and b in the form as + bt. Here 12347 = a(−398721) + b(3188882). The equation GCD(a, b) = as + bt (2) is called Bezout’s Identity. The Euclidean Algorithm not only finds the GCD of a and b but it also finds the integers s and t which satisfy Bezout’s Identity. 1.2 Modular Arithmetic and the Group of Units of Zn We say that an integer a is congruent to b modulo n if n divides a − b. We write a ≡ b mod n. Intuitively, this means that a and b should have the same remainder upon division by n. For example, 13 and 34 are congruent modulo 7. By our definition, this is because 7 divides 34 − 13, but we might also notice that if we divided 13 or 34 by 7, we would get a remainder of 6 in each case. So 34 ≡ 13 ≡ 6 mod 7. We say a and n are relatively prime if GCD(a, n) = 1. If d > 1 is the GCD of a and n, then there is no solution to the equation ax ≡ 1 mod n, for a solution to this equation implies an integer k such that 1 = ax + kn, and any integer dividing a and n must also divide ax + kn. Conversely, if an integer a is relatively prime to an integer n then we may use the Euclidean algorithm to compute s and t such that 1 = as + nt. Then, modulo n, we have that 1 ≡ as, and so s is the inverse 2 of a mod n. We have just proven that a has a multiplicative inverse modulo n if and only if GCD(a, n) = 1, and, furthermore, when this occurs, the Euclidean algorithm in tabular format will reveal the inverse to us! We call the set of integers between 0 and n that are relatively prime to n the set of units of n. and write U (n) := {k ∈ Z : 0 < k < n, GCD(k, n) = 1}. (U (n) is also called the group of units of n.) This set, U (n), is a critical set of study in the arithmetic of computations modulo n. For example, let us set n = 20 and look at U (20) = {1, 3, 7, 9, 11, 13, 17, 19}. We can multiply these number modulo 20. Below (Table 2) is the start of a multiplication table for the units of 20. Table 2: Multiplication modulo 20 · 1 3 7 9 11 13 17 19 1.3 1 1 3 7 9 11 13 17 19 3 3 9 1 7 13 19 11 17 7 7 1 9 9 9 7 11 11 13 1 19 13 13 19 17 17 11 19 19 17 More on the Group of Units of Zn We prove here some basic algebra results for the group of units. First we prove that the product of two units is again (congruent to) a unit. So the group of units is “closed” under multiplication. For example, in U(20), since 7 and 11 are both relatively prime to 20 then so is 77. And thus, when we reduce 77 modulo 20, we will obtain a number (17) already in the group of units. Lemma 1. If i and j are both relatively prime to n then so is ij. Proof. By the Euclidean algorithm, write 1 = is + nt for some integers s and t and write 1 = ju + nv for integers u and v. Then (1)(1) = (is + nt)(ju + nv). Rewrite the right hand side in the form (ij)s0 + (n)t0 for integers s0 and t0 and thus GCD(ij, n) = 1. Exercise for students in an algebra class. Show that U (n) is a group under multiplication. Lemma 2. Suppose a and b are relatively prime. Then if a divides m and b divides m then ab divides m. Proof. Suppose that GCD(a,b)=1 and a|m and b|m. Use the Euclidean algorithm to find integers s, t such that 1 = as + bt. Multiply this equation by m to get m = ams + bmt. 3 Since a|m there exists an integer j such that m = aj. Similarly there is an integer k such that m = bk. This allows us to write the equation for m above in the form m = a(bk)s + b(aj)t. So m = ab(ks + jt). Since ks + jt is an integer, we have ab dividing m. 2 Lemma 3. Suppose m and n are integers with d := GCD(m, n). Then m n and are relatively prime. d d m n m n Proof. Write d = ms + nt for integers s and t. Then 1 = s + t. Since , , s and t are all integers, d d d d m n then, by the converse of the Euclidean algorithm, GCD( , ) = 1. d d 1.3.1 The LCM In order to work further with the set (ring) of integers, we need a few facts about the greatest common divisor and least common multiples of integers. These facts, naturally enough, come out of the Euclidean algorithm. Lemma 4. Suppose m and n are integers with d := GCD(m, n) and l := LCM (m, n). Then l := mn . d n m Proof. Since d|m and d|n then mn/d = m( ) = n( ) is a multiple of both m and n. d d On the other hand, suppose k is a common multiple of both m and n. Write k = mk1 = nk2 for k m n integers k1 , k2 . Then = ( )k1 = ( )k2 . d d d m n k mn k Now and are relatively prime and those both divide . So, by an earlier result, divides . d d d d d d mn divides k. This implies that d mn In other words, is a common multiple of m and n and it divides any other common multiple. So d mn must be the least common multiple of m and n. d Notation. We often write (m, n) for GCD(m, n) and [m, n] for LCD(m, n). Using that notation, the mn lemma above proves that [m, n] = . (m, n) 4 Euclidean Algorithm, Worksheet 1 On all problems below, the instructions “Use the Euclidean Algorithm...” assume that all computations done with the Euclidean Algorithm are done in the tabular format. 1. Finish Table 2 (the multiplication table modulo 20.) 2. Finish the following table, to show that 521 and 641 are relatively prime. Then use your work to find the the integers s and t such that 1 = 641s + 521t. −q -1 -4 -2 as + bt a = 641 b = 521 120 41 s 1 0 1 -4 t 0 1 -1 5 1 0 3. Use the Euclidean algorithm in tabular format to find the GCD of 108 and 605 and compute the integers s and t such that GCD(108, 605) = 108s + 605t. 4. Compute the inverse of 108 in Z605 . 5. Use the Euclidean algorithm to find the integer x such that 1 = 200x + 641y. (The integer x is “the inverse of 200 mod 641.”) 6. Find the “inverse” of 521 modulo 625. (That is, find an integer s such that 521s ≡ 1 mod 625.) 7. Use the Euclidean algorithm to find the GCD of a = 232 + 1 and b = 214 · 52 − 1 and compute the integers s and t such that GCD(a, b) = as + bt. (Fermat, probably around 1640, claimed that 232 + 1 is prime. Was he correct?) 8. Find the smallest pair of integers a and b such that the Euclidean Algorithm, in tabular format, has nine rows. More generally, what is the “worst case” scenario for the Euclidean Algorithm: which pair of integers require the most rows? 9. Use Bezout’s Identity to prove Euclid’s Lemma: If a prime p divides ab then either p divides a or p divides b. (Hint: consider the contrapositive. If p doesn’t divide a and p doesn’t divide b then p is relatively prime to a and relatively prime to b. Can you get from there, via Bezout’s Identity, to the claim that p is relatively prime to ab?) 5 Euclidean Algorithm, Worksheet 2 1. Use the Euclidean algorithm (in tabular format) to find the GCD of 11333 and 7213 and compute the integers s and t such that GCD(11333, 7213) = 11333s + 7213t. 2. Use the Euclidean algorithm to find the GCD of a := (11333)(523) and b := (7213)(523) and compute the integers s and t such that GCD(a, b) = as + bt. 3. Does 7213 have a multiplicative inverse modulo 11333? (If so, what is it?) 4. Describe the “worse case” scenario for the Euclidean algorithm? In particular what would be the smallest pair of numbers that would require 20 steps? 1000 steps? n steps? 5. Use the Euclidean algorithm to find the GCD of a = 123456789 and b = 12345000 and compute the integers s and t such that GCD(a, b) = as + bt. 6. (a) Let a, b ∈ Z. Prove that GCD(a, b) = 1 if and only if there exists integers x, y such that 1 = ax + by. (b) Prove if GCD(a, x) = 1 and GCD(b, x) = 1 then GCD(ab, x) = 1. 7. Use the Euclidean algorithm (in tabular format) to find integers s and t such that 1 = 5s + 1536t. Brief solution. s = 1229, t = −4. 8. Find the multiplicative inverse of 5 modulo 1536. Brief solution. 5−1 = 1229 mod 1536. 9. Use the concept of repeated squaring to compute 6695 modulo 1649. Brief solution. 6692 ≡ 682 2 682 ≡ 106 mod 1649 mod 1649 Therefore 6695 = (6694 )(669) ≡ (106)(669) ≡ 7 mod 1649. 10. If I square the number 7, and then keep squaring the squares modulo 1649, I get the following list. 72 ≡ 49 mod 1649 74 = 492 ≡ 752 mod 1649 78 = 7522 ≡ 1546 mod 1649 716 = 15462 ≡ 715 mod 1649 732 = 7152 ≡ 35 mod 1649 764 = 352 ≡ 1225 mod 1649 7128 = 12252 ≡ 35 mod 1649 6 7256 = 352 ≡ 1225 .. . mod 1649 Use this list to compute 71229 mod 1649. Brief solution. 71229 = (71024 )(7128 )(764 )(78 )(74 )(71 ) ≡ (1225)(35)(1225)(1546)(752)(7) ≡ 669 mod 1649. 11. Factor 1649. Brief solution. 1649 = 17 · 97. Comment. Notice that 71229 ≡ 669 mod 1649 and 6695 ≡ 7 mod 1649. More generally, for most integers M < 1229, if S ≡ M 1229 mod 1649 then M ≡ S5 mod 1649. Given M we can create S; given S we can recover M . The function f (M ) = M 1229 mod 1649 has an inverse function g(S) = S 5 mod 1649. Why? This is because 5 and 1229 are inverses modulo 1536. (What is the relationship between 1649 and 1536??) More later.... 7