Great Theoretical Ideas In CS Victor Adamchik CS - Lecture Carnegie Mellon University Outline Number Theory and Modular Arithmetic p- p Working modulo integer n Definitions of Z n, Z n Fundamental lemmas of +,-,,/ Extended Euclid Algorithm Euler phi function (n) = Z n Fundamental lemma of powers Euler Theorem (a mod n) means the remainder when a is divided by n. Definition: Modular equivalence a b [mod n] (a mod n) = (b mod n) n (a-b) a mod n = r a = d n + r for some integer d or a = n + r k for some integer k 8 [mod ] 8 8 [mod 7] 7 8 Written as a n b, and spoken a and b are equivalent or congruent modulo n n induces a natural partition of the integers into n residue classes. ( residue = what left over = remainder ) Define residue class [k] = the set of all integers that are congruent to k modulo n. Residue Classes Mod : [] = {, -, -,,,,..} [] = {, -, -,,, 7,..} [] = {, -, -,,, 8,..} [-] = {, -, -,,,,..} [7] = {, -, -,,, 7,..} [-] = {, -, -,,, 8,..} = [] = [] = []
n is an equivalence relation In other words, it is Reflexive: a n a Symmetric: (a n b) (b n a) Transitive: (a n b and b n c) (a n c) Why do we care about these residue classes? Because we can replace any member of a residue class with another member when doing addition or multiplication mod n and the answer will not change To calculate: 9 mod just do - = - = 7 Fundamental lemma of plus and times mod n: If (x n y) and (a n b). Then ) x + a n y + b ) x a n y b Proof of ): x a = y b (mod n) (x n y) => x = y + k n (a n b) => a = b + m n x a = y b + n (y m + b k + k m) Proof: Another Simple Fact: if (x n y) and (k n), then: x k y Example: x = y + m n n = a k x = y + a m k x k y A Unique Representation System Modulo n: We pick one representative from each residue class and do all our calculations using these representatives. Unsurprisingly, we use,,,, n-
Unique representation system mod Finite set Z = {, } Unique representation system mod Finite set Z = {,,, } + XOR AND + Notation Z n = {,,,, n-} Define operations + n and n : a + n b = (a + b mod n) a n b = (a b mod n) Some properties of the operation + n [ Closed ] x, y Z n x + n y Z n [ Associative ] x, y, z Z n (x + n y) + n z = x + n (y + n z) [ Commutative ] x, y Z n x + n y = y + n x Similar properties also hold for n For addition tables, rows and columns always are a permutation of Z n For multiplication, some rows and columns are permutation of Z n, while others aren t + + what s happening here?
For addition, the permutation property means you can solve, say, + x = (mod ) For multiplication, if a row has a permutation you can solve, say, x = (mod ) Subtraction mod n is well-defined Each row has a, hence a is that element such that a + (-a) = a b = a + (-b) + x = (mod ) no solutions! x = (mod ) multiple solutions! Multiplicative Inverse Definition. Let a in Z n An element b in Z n is called a multiplicative inverse of a, if a b = (mod n) A visual way to understand multiplication and the permutation property. Consider 8 on Z 8 7 7 7 7 7 There are exactly 8 distinct multiples of modulo 8. 7 k mod 8 hit all numbers row has the permutation property
There are exactly distinct multiples of modulo 8. 7 k mod 8 There are exactly distinct multiples of 8 modulo 8. 7 8k mod 8 row does not have permutation property for 8 on Z 8 There are exactly distinct multiples of modulo 8. What s the pattern? 7 k mod 8 exactly 8 distinct multiples of modulo 8 exactly distinct multiples of modulo 8 exactly distinct multiple of 8 modulo 8 exactly distinct multiples of modulo 8 y/gcd(x,y) exactly distinct multiples of x modulo y Theorem: There are exactly y/gcd(x,y) distinct multiples of x modulo y Hence, only those values of x with GCD(x,y) = have n distinct multiples (i.e., the permutation property for n on Z n ) Fundamental lemma of division (or cancelation) modulo n: if GCD(c,n)=, then ca n cb a n b Proof: c a = n c b => n (ca cb) => n c(a-b) But GCD(n, c)=, thus n (a-b) => a = n b
Fundamental lemmas mod n: If you want to extend to general c and n If (x n y) and (a n b). Then ca n cb a n/gcd(c,n) b ) x + a n y + b ) x a n y b ) x - a n y b ) cx n cy a n b if gcd(c,n)= New definition: Z = {,,,,,} Z = {,} Z n = {x Z n GCD(x, n) =} + Multiplication over this set Z n has the cancellation property. We ve got closure Z = { x < gcd(x,) = } Recall we proved that Z n was closed under addition and multiplication? What about Z n under multiplication? Fact: if a,b in Z n,then ab in Z n Proof: if gcd(a,n) = gcd(b,n) =, then gcd(a b, n) = then gcd(a b mod n, n) = = {,,7,} 7 7 7 7 7 7
Z = {,,,} = Z \ {} For prime p, the set Z p = Z p \ {} Proof: It just follows from the definition! For prime p, all < x < p satisfy gcd(x,p) = Euler Phi Function (n) (n) = size of Z n = number of k < n that are relatively prime to n. p prime Z p = {,,,,p-} (p) = p- Z = { x < gcd(x,) = } = {,,7,} () = 7 7 7 7 7 7 Theorem: if p,q distinct primes then (p q) = (p-)(q-) pq = # of numbers from to pq p = # of multiples of q up to pq q = # of multiples of p up to pq = # of multiple of both p and q up to pq (pq) = pq p q + = (p-)(q-) () = () =(-)(-)=8 Multiplicative inverse of a mod n = number b such that ab= (mod n) Remember, only defined for numbers a in Z n What is the multiplicative inverse of a = 9 in Z 9 = Z n? Answer: a - = 879 7
How do you find multiplicative inverses fast? Theorem: given positive integers X, Y, there exist integers r, s such that r X + s Y = gcd(x, Y) and we can find these integers fast! Extended Euclid Algorithm Now take n, and a in Z n gcd(a, n)? a in Z n gcd(a, n) = Thus, we can find r and s s.t. ra + sn = then ra = n so, r = a - mod n Euclid s Algorithm for GCD Euclid(A,B) If B= then return A else return Euclid(B, A mod B) Extended Euclid Algorithm Let <r,s> denote the number r7 + s9 =. Calculate all intermediate values in this representation. Euclid(7,9) 7 9 = 7 mod 9 = 9 Euclid(9,9) 9 9 = 9 mod 9 = Euclid(9,) 9 = 9 mod = Euclid(,) = mod = Euclid(,) outputs 7=<,> 9=<,> Euclid(7,9) 9=<,> <,> 9 =<-, -> Euclid(9,9) =<,> <,-> =<-,+> Euclid(9,) =<,-> <-,7> =<,-> Euclid(,) =<-,7> <,-> =<-9,7> Euclid(,) outputs = 7 9 Finally, a puzzle Diophantine equation You have a gallon bottle, a gallon bottle, and lots of water. Can you measure out exactly gallons? Does the equality x + y = have a solution where x,y are integers? 8
New bottles of water puzzle Theorem You have a gallon bottle, a gallon bottle, and lots of water. How can you measure out exactly gallons? The linear equation a x + b y = c has an integer solution in x and y iff gcd(a,b) c The linear equation a x + b y = c has an integer solution in x and y iff gcd(a,b) c Hilbert s th problem =>) gcd(a,b) a and gcd(a,b) b => gcd(a,b) (a x + b y) <=) gcd(a,b) c => c = z gcd(a,b) Hilbert asked for a universal method of solving all Diophantine equations P(x,x,,x n )= with any number of unknowns and integer coefficients. On the other hand, gcd(a,b) = x a + y b z gcd(a,b) = z x a + z y b c = z x a + z y b In 97 Y. Matiyasevich proved that the Diophantine problem is unsolvable. How do you compute Exponentiation 8 using few multiplications? First idea: 7 8 = = 9
8 How do you compute Repeated squaring calculates a k in k multiply operations Better idea: 8 = = = Used only mults instead of 7!!! compare with ( k ) multiply operations used by the naïve method How do you compute To compute a m Use repeated squaring again? 8 Note that = 8++ = () So a = a 8 a a Two more multiplies! Suppose k m < k+ a a a a 8... a k This takes k multiplies Now write m as a sum of distinct powers of say, m = k + i + i + i t a m = a k a i a it at most k more multiplies How do you compute Hence, we can compute a m while performing at most log m multiplies (mod ) First idea: Compute using multiplies 8 = 7 = 8 = then take the answer mod 7 (mod ) =
(mod ) How do you compute Better idea: keep reducing the answer mod 8 8 = = = = = 9 = = = Hence, we can compute a m (mod n) while performing at most log m multiplies where each time we multiply together numbers with log n + bits How do you compute (mod ) The current best idea would still need about calculations OK, need a little more number theory for this one answer = Can we exponentiate any faster? Fundamental lemma of powers? If (x n y) Then a x n a y? NO! ( ), but it is not the case that: (Correct) Fundamental lemma of powers. If a Z n and x (n) y then a x n a y Equivalently, for a Z n, a x n ax mod (n)
How do you compute (mod ) (mod ) = for a Z n, a x n ax mod (n) Hence, we can compute a m (mod n) while performing at most log (n) multiplies (mod ) = mod = for a Z n, a x n ax mod (n) Why did we take mod? where each time we multiply together numbers with log n + bits 8 787 mod 9 Step : reduce the base mod 9 Step : reduce the exponent mod (9) = you should check that gcd(8,9)= to use lemma of powers Step : use repeated squaring to compute, taking mods at each step How do you prove the lemma for powers? for a Z n, a x n ax mod (n) Use Euler s Theorem For a Z n, a (n) n Corollary: Fermat s Little Theorem For p prime, a Z p a p- p Proof of Euler s Theorem: for a Z n, a (n) n Define a Z n = {a n x x Z n } for a Z n By the permutation property, Z n = az n x n ax [as x ranges over Z n ] x n x (a size of Zn ) [Commutativity] = n a size of Zn [Cancellation] a (n) = n Here s What You Need to Know Working modulo integer n Definitions of Z n, Z n Fundamental lemmas of +,-,,/ Extended Euclid Algorithm Euler phi function (n) = Z n Fundamental lemma of powers Euler Theorem