Example of Extended Eucldean Algorthm Recall that gcd(84, 33) = gcd(33, 18) = gcd(18, 15) = gcd(15, 3) = gcd(3, 0) = 3 We work backwards to wrte 3 as a lnear combnaton of 84 and 33: 3 = 18 15 [Now 3 s a lnear combnaton of 18 and 15] = 18 (33 18) = 2(18) 33 [Now 3 s a lnear combnaton of 18 and 33] = 2(84 2 33)) 33 = 2 84 5 33 [Now 3 s a lnear combnaton of 84 and 33] Some Consequences Corollary 2: If a and b are relatvely prme, then there exst s and t such that as + bt = 1. Corollary 3: If gcd(a, b) = 1 and a bc, then a c. Proof: Exst s, t Z such that sa + tb = 1 Multply both sdes by c: sac + tbc = c Snce a bc, a sac + tbc, so a c Corollary 4: If p s prme and p Π n =1 a, then p a for some 1 n. Proof: By nducton on n: If n = 1: trval. Suppose the result holds for n and p Π n+1 =1 a. note that p Π n+1 =1 a = (Π n =1 a )a n+1. If p a n+1 we are done. If not, gcd(p, a n+1 ) = 1. By Corollary 3, p Π n =1 a By the IH, p a for some 1 n. 1 2 The Fundamental Theorem of Arthmetc, II Theorem 3: Every n > 1 can be represented unquely as a product of prmes, wrtten n nondecreasng sze. Proof: Stll need to prove unqueness. We do t by strong nducton. Base case: Obvous f n = 2. Inductve step. Suppose OK for n < n. Suppose that n = Π s =1 p = Π r j=1 q j. p 1 Π r j=1 q j, so by Corollary 4, p 1 q j for some j. But then p 1 = q j, snce both p 1 and q j are prme. But then n/p 1 = p 2 p s = q 1 q j 1 q j+1 q r Result now follows from I.H. Characterzng the GCD and LCM Theorem 6: Suppose a = Π n =1 p α where p are prmes and α, β N. Some α s, β s could be 0. Then gcd(a, b) = Π n =1 p mn(α,β ) lcm(a, b) = Π n =1 p max(α,β ) Proof: For gcd, let c = Π n =1 p mn(α,β ). Clearly c a and c b. and b = Π n =1 p β, Thus, c s a common dvsor, so c gcd(a, b). If q γ gcd(a, b), must have q {p 1,..., p n } Otherwse q a so q gcd(a, b) (lkewse b) If q = p, q γ gcd(a, b), must have γ mn(α, β ) E.g., f γ > α, then p γ a Thus, c gcd(a, b). Concluson: c = gcd(a, b). 3 4
For lcm, let d = Π n =1 p max(α,β ). Clearly a d, b d, so d s a common multple. Thus, d lcm(a, b). Suppose lcm(a, b) = Π n =1 p γ. Must have α γ, snce p α Smlarly, must have β γ. Thus, max(α, β ) γ. Concluson: d = lcm(a, b). a and a lcm(a, b). Example: 432 = 2 4 3 3, and 95256 = 2 3 3 5 7 2, so gcd(95256, 432) = 2 3 3 3 = 216 lcm(95256, 432) = 2 4 3 5 7 2 = 190512. Corollary 5: ab = gcd(a, b) lcm(a, b) Proof: mn(α, β) + max(α, β) = α + β. Example: 4 10 = 2 20 = gcd(4, 10) lcm(4, 10). Modular Arthmetc Remember: a b (mod m) means a and b have the same remander when dvded by m. Equvalently: a b (mod m) ff m (a b) a s congruent to b mod m Theorem 7: If a 1 a 2 (mod m) and b 1 b 2 (mod m), then (a) (a 1 + b 1 ) (a 2 + b 2 ) (mod m) (b) a 1 b 1 a 2 b 2 (mod m) Proof: Suppose So a 1 = c 1 m + r, a 2 = c 2 m + r b 1 = d 1 m + r, b 2 = d 2 m + r a 1 + b 1 = (c 1 + d 1 )m + (r + r ) a 2 + b 2 = (c 2 + d 2 )m + (r + r ) m ((a 1 + b 1 ) (a 2 + b 2 ) = ((c 1 + d 1 ) (c 2 + d 2 ))m Concluson: a 1 + b 1 a 2 + b 2 (mod m). 5 6 For multplcaton: a 1 b 1 = (c 1 d 1 m + r c 1 + rd 1 )m + rr a 2 b 2 = (c 2 d 2 m + r c 2 + rd 2 )m + rr m (a 1 b 1 a 2 b 2 ) Concluson: a 1 b 1 a 2 b 2 (mod m). Bottom lne: addton and multplcaton carry over to the modular world. Modular arthmetc has lots of applcatons. Here are four... Hashng Problem: How can we effcently store, retreve, and delete records from a large database? For example, students records. Assume, each record has a unque key E.g. student ID, Socal Securty # Do we keep an array sorted by the key? Easy retreval but dffcult nserton and deleton. How about a table wth an entry for every possble key? Often nfeasble, almost always wasteful. There are 10 10 possble socal securty numbers. Soluton: store the records n an array of sze N, where N s somewhat bgger than the expected number of records. Store record wth d k n locaton h(k) h s the hash functon Basc hash functon: h(k) := k (mod N). A collson occurs when h(k 1 ) = h(k 2 ) and k 1 k 2. Choose N suffcently large to mnmze collsons Lots of technques for dealng wth collsons 7 8
Pseudorandom Sequences ISBN Numbers For randomzed algorthms we need a random number generator. Most languages provde you wth a functon rand. There s nothng random about rand! It creates an apparently random sequence determnstcally These are called pseudorandom sequences A standard technque for creatng psuedorandom sequences: the lnear congruental method. Choose a modulus m N +, a multpler a {2, 3,..., m 1}, and an ncrement c Z m = {0, 1,..., m 1}. Choose a seed x 0 Z m Typcally the tme on some nternal clock s used Compute x n+1 = ax n + c (mod m). Warnng: a poorly mplemented rand, such as n C, can wreak havoc on Monte Carlo smulatons. 9 Snce 1968, most publshed books have been assgned a 10-dgt ISBN numbers: dentfes country of publcaton, publsher, and book tself The ISBN number for DAM3 s 1-56881-166-7 All the nformaton s encoded n the frst 9 dgts The 10th dgt s used as a party check If the dgts are a 1,..., a 10, then we must have a 1 + 2a 2 + + 9a 9 + 10a 10 0 (mod 11). For DAM3, get 1 + 2 5 + 3 6 + 4 8 + 5 8 + 6 1 +7 1 + 8 6 + 9 6 + 10 7 = 286 0 (mod 11) Ths test always detects errors n sngle dgts and transposton errors Two arbtrary errors may cancel out Smlar party checks are used n unversal product codes (UPC codes/bar codes) that appear on almost all tems The numbers are encoded by thcknesses of bars, to make them machne readable 10 Castng out 9s Lnear Congruences Notce that a number s equvalent to the sum of ts dgts mod 9. Ths can be used as a way of checkng your addton and of dong mndreadng [come to class to hear more... ] The equaton ax = b for a, b R s unquely solvable f a 0: x = ba 1. Can we also (unquely) solve ax b (mod m)? If x 0 s a soluton, then so s x 0 + km k Z... snce km 0 (mod m). So, unqueness can only be mod m. But even mod m, there can be more than one soluton: Consder 2x 2 (mod 4) Clearly x 1 (mod 4) s one soluton But so s x 3 (mod 4)! Theorem 8: If gcd(a, m) = 1 then there s a unque soluton (mod m) to ax b (mod m). Proof: Suppose r, s Z both solve the equaton: then ar as (mod m), so m a(r s) Snce gcd(a, m) = 1, by Corollary 3, m (r s) But that means r s (mod m) So f there s a soluton at all, then t s unque mod m. 11 12
Solvng Lnear Congruences The Chnese Remander Theorem But why s there a soluton to ax b (mod m)? Key dea: fnd a 1 mod m; then x ba 1 (mod m) By Corollary 2, snce gcd(a, m) = 1, there exst s, t such that as + mt = 1 So as 1 (mod m) That means s a 1 (mod m) x bs (mod m) Suppose we want to solve a system of lnear congruences: Example: Fnd x such that x 2 (mod 3) x 3 (mod 5) x 2 (mod 7) Can we solve for x? Is the answer unque? Defnton: m 1,..., m n are parwse relatvely prme f each par m, m j s relatvely prme. Theorem 9 (Chnese Remander Theorem): Let m 1,..., m n N + be parwse relatvely prme. The system x a (mod m ) = 1, 2... n (1) has a unque soluton modulo M = Π n 1m. The best we can hope for s unqueness modulo M: If x s a soluton then so s x + km for any k Z. Proof: Frst I show that there s a soluton; then I ll show t s unque. 13 14 CRT: Exstence CRT: Example Key dea for exstence: Suppose we can fnd y 1,..., y n such that Now consder y := Σ n j=1 y j. y a (mod m ) y 0 (mod m j ) f j. Σ n j=1 y j a (mod m ) Snce y = a mod m and y j = 0 mod m j f j. So y s a soluton! Now we need to fnd y 1,..., y n. Let M = M/m = m 1 m 1 m +1 m n. gcd(m, m ) = 1, snce m j s parwse relatvely prme No common prme factors among any of the m j s Choose y such that (M )y a (mod m ) Can do that by Theorem 8, snce gcd(m, m ) = 1. Let y = y M. y s a multple of m j f j, so y 0 (mod m j ) y = y M a (mod m ) by constructon. So y 1 + + y n s a soluton to the system, mod M. 15 Fnd x such that x 2 (mod 3) x 3 (mod 5) x 2 (mod 7) Fnd y 1 such that y 1 2 (mod 3), y 1 0 (mod 5/7): y 1 has the form y 1 5 7 35y 1 2 (mod 3) y 1 = 1, so y 1 = 35. Fnd y 2 such that y 2 3 (mod 5), y 2 0 (mod 3/7): y 2 has the form y 2 3 7 21y 2 3 (mod 5) y 2 = 3, so y 2 = 63. Fnd y 3 such that y 3 2 (mod 7), y 3 0 (mod 3/5): y 3 has the form y 3 3 5 15y 3 2 (mod 7) y 3 = 2, so y 3 = 30. Soluton s x = y 1 + y 2 + y 3 = 35 + 63 + 30 = 128 16
CRT: Unqueness What f x, y are both solutons to the equatons? x y (mod m ) m (x y), for = 1,..., n Clam: M = m 1 m n (x y) so x y (mod M) Theorem 10: If m 1,..., m n are parwse relatvely prme and m b for = 1,..., n, then m 1 m n b. Proof: By nducton on n. For n = 1 the statement s trval. Suppose statement holds for n = N. Suppose m 1,..., m N+1 relatvely prme, m b for = 1,..., N + 1. by IH, m 1 m N b b = m 1 m N c for some c By assumpton, m N+1 b, so m (m 1 m N )c gcd(m 1 m N, m N+1 ) = 1 (snce m s parwse relatvely prme no common factors) by Corollary 3, m N+1 c so c = dm N+1, b = m 1 m N m N+1 d so m 1 m N+1 b. An Applcaton of CRT: Computer Arthmetc wth Large Integers Suppose we want to perform arthmetc operatons (addton, multplcaton) wth extremely large ntegers too large to be represented easly n a computer Idea: Step 1: Fnd sutable modul m 1,..., m n so that m s are relatvely prme and m 1 m n s bgger than the answer. Step 2: Perform all the operatons mod m j, j = 1,..., n. Ths means we re workng wth much smaller numbers (no bgger than m j ) The operatons are much faster Can do ths n parallel Suppose the answer mod m j s a j : Use CRT to fnd x such that x a j (mod m j ) The unque x such that 0 < x < m 1 m n s the answer to the orgnal problem. 17 18 Example: The followng are parwse relatvely prme: 2 35 1, 2 34 1, 2 33 1, 2 29 1, 2 23 1 We can add and multply postve ntegers up to (2 35 1)(2 34 1)(2 33 1)(2 29 1)(2 23 1) > 2 163. 19 Fermat s Lttle Theorem Theorem 11 (Fermat s Lttle Theorem): (a) If p prme and gcd(p, a) = 1, then a p 1 1 (mod p). (b) For all a Z, a p a (mod p). Proof. Let A = {1, 2,..., p 1} B = {1a mod p, 2a mod p,..., (p 1)a mod p} Clam: A = B. 0 / B, snce p ja, so B A. If j, then a mod p ja mod p snce p (j )a Thus A = p 1, so A = B. Therefore, Π A Π B (mod p) (p 1)! a(2a) (p 1)a = (p 1)! a p 1 (mod p) p (a p 1 1)(p 1)! p (a p 1 1) [snce gcd(p, (p 1)!) = 1] a p 1 1 (mod p) It follows that a p a (mod p) Ths s true even f gcd(p, a) 1;.e., f p a Why s ths beng taught n a CS course? 20