CS250: Discrete Math for Computer Science L6: Euclid s Algorithm & Multiplicative Inverses Mod m
Greatest Common Divisors, GCD If d a and d b then d is a common divisor of a and b. 1, 2, 3, and 6 are common divisors of 12,18. 1 is a common divisor of every pair of integers a,b. The greatest common divisor of a,b is denoted gcd(a,b). gcd(12, 18) = 6 gcd(5,11) = 1 5 and 11 are relatively prime gcd(17, 34) = 17 gcd(30, 100) = 10 gcd(98, 105) = 7
How do we efficiently compute gcd(a, b)? Easy if we know the prime factors of a and b: 12 = 2 2 3 1 18 = 2 1 3 2 gcd(12,18) = 2 1 3 1 5 = 5 1 11 0 11 = 5 0 11 1 gcd(5,11) = 5 0 11 0 17 = 2 0 17 1 34 = 2 1 17 1 gcd(17,34) = 2 0 17 1 30 = 2 1 3 1 5 1 100 = 2 2 3 0 5 2 gcd(30,100) = 2 1 3 0 5 1 98 = 2 1 30 50 72 105 = 2 0 31 51 71 gcd(98,105) = 20 30 50 71 Prop: If a = p a 1 1 pa 2 2 pa k k and b = p b 1 for primes p 1 < p 2 < < p k, 1 pb 2 2 pb k Then gcd(a,b) = p min(a 1,b 1 ) 1 p min(a 2,b 2 ) 2 p min(a k,b k ) k k
But, factoring integers is computationally difficult To factor a thousand-bit integer, a, we would try all divisors up to a but that we would be about 2 500 divisors! This is exponential time in terms of the size of the input, so it is not feasible. Today, we will see how over 2300 years ago, Euclid gave a very efficient algorithm to compute gcd(a,b), without factoring. This was in Euclid s Geometry text. He was thinking about line segments and wanted to be able to compute the length d of the longest line segment that evenly divided two given line segments, a and b.
Euclid s algorithm To compute: gcd(12, 18) = gcd(18, 12), Divide the bigger number, b, by the smaller, s, computing the remainder, r. Repeat until remainder = 0. Answer = the last postive remainder. 18 = 1 12 + 6 Answer: gcd(18,12) = 6 12 = 2 6 + 0
Euclid s algorithm Compute: gcd(123, 42) 123 = 2 42 + 39 42 = 1 39 + 3 Answer: gcd(123,42) = 3 39 = 13 3 + 0 Compute: gcd(13, 8) 13 = 1 8 + 5 8 = 1 5 + 3 5 = 1 3 + 2 3 = 1 2 + 1 Answer: gcd(13,8) = 1 2 = 2 1 + 0
Euclid s Algorithm: Why It Works Algorithm: GCD(b, s) Input: integers b > s 0 1. while (s 0) do { b := s; s := (b % s) } 2. return(b) Lemma [Euclid, 300 B.C.] If b > s > 0 Then gcd(b,s) = gcd(s,(b % s)). Each iteration of the while loop decreases s. Thus Euclid s Algorithm eventually halts. By Euclid s Lemma, each iteration preserves value of gcd(b,s). Correct answer at last step: because gcd(b,0) = b because b 0 (b 0 = 0) Thus, Euclid s Algorithm (GCD) correctly computes gcd.
Lemma [Euclid, 300 B.C.] If b > s > 0 Then gcd(b,s) = gcd(s,(b % s)). proof: Let r = b % s and b = q s + r Claim: d[(d b d s) (d s d r)] let d be arbitrary suppose (d b d s) Thus, d (b q s), i.e., d r. Conversely, suppose (d s d r) Thus, d (q s + r), i.e., d b. This proves the claims. Thus gcd(b, s) = gcd(s, r).
Euclid s Algorithm: How Long Does it Take? Algorithm: GCD(b, s) Input: integers b > s 0 1. while (s 0) do { b := s; s := (b % s) } 2. return(b) Claim: After two iterations: b b/2. b := s; s := (b % s) b := s ; s := (b % s ) proof: Suppose s b/2, then reduced by half after one iteration. Otherwise: s > b/2. Thus, s = b%s = b s so s < b/2. Thus b = s < b/2 Thus, Euclid s Algorithm takes at most 2 log b iterations. Thus linear, i.e., O(n) iterations where n = b is the number of bits to represent b.
Euclid s Algorithm Backwards Thm: ab xy(ax + by = gcd(a,b)) Remember that our universe of discourse is Z. 18 = 1 12 + 6 gcd(18,12) = 6 Express gcd(a,b) in terms of previous values. 6 = 18 1 + 12 ( 1)
ab xy(ax + by = gcd(a, b)) 123 = 2 42 + 39 42 = 1 39 + 3 gcd(123,42) = 3 Express gcd(a,b) in terms of previous values; regroup; repeat. 3 = 42 1 + 39 ( 1) 3 = 42 1 + (123 + 42 ( 2)) ( 1) 3 = 123 ( 1) + 42 3
ab xy(ax + by = gcd(a, b)) 13 = 1 8 + 5 8 = 1 5 + 3 5 = 1 3 + 2 3 = 1 2 + 1 gcd(13,8) = 1 Express gcd(a,b) in terms of previous values; regroup; repeat. 1 = 3 1 + 2 ( 1) 1 = 3 1 + (5 + 3 ( 1)) ( 1) 1 = 5 ( 1) + 3 2 1 = 5 ( 1) + (8 + 5 ( 1)) 2 1 = 8 (2) + 5 3 1 = 8 (2) + (13 + 8 ( 1)) 3 1 = 13 ( 3) + 8 5 Don t forget to check: 1 = 39 + 40
ab xy(ax + by = gcd(a, b)) In fact, x and y can be computed very efficiently by running Euclid s Algorithm backwards. 13 ( 3) 1 (mod 8) 8 (5) 1 (mod 13) 1 = 13 ( 3) + 8 5 Thm: If gcd(a,b) = 1 then we can efficiently compute the multiplicative inverse of a (mod b). Proof. It s x in the above equation. ax + by = 1 ax 1(mod b)
Multipicative Inverses Mod m Thm: For all integers a,m s.t. m > 1, a has a multiplicative inverse mod m iff a and m are relatively prime. Proof: Recall a and m are relatively prime iff gcd(a,m) = 1. Let a and m be arbitrary with m > 1. Assume: gcd(a,m) = 1 Then xy(ax + my = 1) so x is mult. inv. of a (mod m). Assume: ax 1(mod m) Thus, y(ax + my = 1). If, d a and d m, then d (ax + my), i.e., d 1. Thus, gcd(a,m) = 1. Since a and m were arbitrary, Thm holds for all such a,m.
a, m > 1 (a has mult. inverse mod m iff gcd(a, m) = 1) mod 6 0 1 2 3 4 5 0 0 0 0 0 0 0 1 0 1 2 3 4 5 2 0 2 4 0 2 4 3 0 3 0 3 0 3 4 0 4 2 0 4 2 5 0 5 4 3 2 1