cse 311: foundations of computing Spring 2015 Lecture 11: Modular arithmetic and applications
arithmetic mod 7 0 6 1 a + 7 b = (a + b) mod 7 2 5 a 7 b = (a b) mod 7 3 4 + 0 1 2 3 4 5 6 0 1 2 3 4 5 6 X 0 0 1 2 3 4 5 6 0 0 0 0 0 0 0 0 1 1 2 3 4 5 6 0 1 0 1 2 3 4 5 6 2 2 3 4 5 6 0 1 2 0 2 4 6 1 3 5 3 3 4 5 6 0 1 2 3 0 3 6 2 5 1 4 4 4 5 6 0 1 2 3 4 0 4 1 5 2 6 3 5 5 6 0 1 2 3 4 5 0 5 3 1 6 4 2 6 6 0 1 2 3 4 5 6 0 6 5 4 3 2 1
review: division theorem Let a be an integer and d a positive integer. Then there are unique integers q and r , with 0 ≤ r < d , such that a = d q + r . q = a div d r = a mod d Note: r ≥ 0 even if a < 0. Not quite the same as a % d.
review: modular congruence Let a and b be integers, and m be a positive integer. We say a is congruent to b modulo m if m divides a – b . We use the notation a ≡ b (mod m) to indicate that a is congruent to b modulo m.
modular arithmetic: examples A ≡ 0 (mod 2) This statement is the same as saying “A is even”; so, any A that is even (including negative even numbers) will work. 1 ≡ 0 (mod 4) This statement is false. If we take it mod 1 instead, then the statement is true. A ≡ -1 (mod 17) If A = 17x – 1 = 17(x-1) + 16 for an integer x, then it works. Note that (m – 1) mod m = ((m mod m) + (-1 mod m)) mod m = (0 + -1) mod m = -1 mod m
congruence and residues Theorem: Let a and b be integers, and let m be a positive integer. Then a ≡ b (mod m) if and only if a mod m = b mod m. Proof:
congruence and residues Theorem: Let a and b be integers, and let m be a positive integer. Then a ≡ b (mod m) if and only if a mod m = b mod m. Proof: ⇒ Suppose that a ≡ b (mod m). By definition: a ≡ b (mod m) implies m | (a – b) which by definition implies that a – b = km for some integer k. Therefore a = b + km. Taking both sides modulo m we get a mod m = (b+km) mod m = b mod m
congruence and residues Theorem: Let a and b be integers, and let m be a positive integer. Then a ≡ b (mod m) if and only if a mod m = b mod m. Proof:
congruence and residues Theorem: Let a and b be integers, and let m be a positive integer. Then a ≡ b (mod m) if and only if a mod m = b mod m. Proof: ⇐ Suppose that a mod m = b mod m. By the division theorem, a = mq + (a mod m) and b = ms + (b mod m) for some integers q,s. a – b = (mq + (a mod m)) – (mr + (b mod m)) = m(q – r) + (a mod m – b mod m) = m(q – r) since a mod m = b mod m Therefore m | (a-b) and so 𝑏 ≡ 𝑐 (mod 𝑛)
consistency of addition Let m be a positive integer. If a ≡ b (mod m) and c ≡ d (mod m), then a + c ≡ b + d (mod m )
consistency of addition Let m be a positive integer. If a ≡ b (mod m) and c ≡ d (mod m), then a + c ≡ b + d (mod m ) Suppose a ≡ b (mod m) and c ≡ d (mod m). Unrolling definitions gives us some k such that a – b = km, and some j such that c – d = jm. Adding the equations together gives us (a + c) – (b + d) = m(k + j). Now, re-applying the definition of mod gives us a + c ≡ b + d (mod m).
consistency of multiplication Let m be a positive integer. If a ≡ b (mod m) and c ≡ d (mod m), then ac ≡ bd (mod m) Suppose a ≡ b (mod m) and c ≡ d (mod m). Unrolling definitions gives us some k such that a – b = km, and some j such that c – d = jm. Then, a = km + b and c = jm + d. Multiplying both together gives us ac = (km + b)(jm + d) = kjm 2 + kmd + jmb + bd Rearranging gives us ac – bd = m(kjm + kd + jb). Using the definition of mod gives us ac ≡ bd (mod m).
example Let 𝑜 be an integer. Prove that 𝑜 2 ≡ 0 (mod 4) or 𝑜 2 ≡ 1 (mod 4)
example Let 𝑜 be an integer. Prove that 𝑜 2 ≡ 0 (mod 4) or 𝑜 2 ≡ 1 (mod 4) Case 1 (n is even): Suppose n ≡ 0 (mod 2). Then, n = 2k for some integer k. So, n 2 = (2k) 2 = 4k 2 . So, by definition of congruence, n 2 ≡ 0 (mod 4). Case 2 (n is odd): Suppose n ≡ 1 (mod 2). Then, n = 2k + 1 for some integer k. So, n 2 = (2k + 1) 2 = 4k 2 + 4k + 1 = 4(k 2 + k) + 1. So, by definition of congruence, n 2 ≡ 1 (mod 4).
n-bit unsigned integer representation • Represent integer x as sum of powers of 2: 𝑜−1 𝑐 𝑗 2 𝑗 where each b i ∈ {0,1} If 𝑦 = 𝑗=0 then representation is b n-1 ⋯ b 2 b 1 b 0 99 = 64 + 32 + 2 + 1 18 = 16 + 2 • For n = 8: 99: 0110 0011 18: 0001 0010
sign-magnitude integer representation n-bit signed integers Suppose −2 𝑜−1 < 𝑦 < 2 𝑜−1 First bit as the sign, n-1 bits for the value 99 = 64 + 32 + 2 + 1 18 = 16 + 2 For n = 8: 99: 0110 0011 -18: 1001 0010 Any problems with this representation?
two’s complement representation n-bit signed integers, first bit will still be the sign bit Suppose 0 ≤ 𝑦 < 2 𝑜−1 , 𝑦 is represented by the binary representation of 𝑦 Suppose 0 ≤ 𝑦 ≤ 2 𝑜−1 , −𝑦 is represented by the binary representation of 2 𝑜 − 𝑦 Key property: Two’s complement representation of any number y is equivalent to y mod 2 n so arithmetic works mod 2 n 99 = 64 + 32 + 2 + 1 18 = 16 + 2 For n = 8: 99: 0110 0011 -18: 1110 1110
sign-magnitude vs . two’s complement -7 -6 -5 -4 -3 -2 -1 0 1 2 3 4 5 6 7 1111 1110 1101 1100 1011 1010 1001 0000 0001 0010 0011 0100 0101 0110 0111 Sign-Magnitude -8 -7 -6 -5 -4 -3 -2 -1 0 1 2 3 4 5 6 7 1000 1001 1010 1011 1100 1101 1110 1111 0000 0001 0010 0011 0100 0101 0110 0111 Two’s complement
two’s complement representation • For 0 < 𝑦 ≤ 2 𝑜−1 , −𝑦 is represented by the binary representation of 2 𝑜 − 𝑦 • To compute this: Flip the bits of 𝑦 then add 1: – All 1’s string is 2 𝑜 − 1 , so Flip the bits of 𝑦 replace 𝑦 by 2 𝑜 − 1 − 𝑦
basic applications of mod • Hashing • Pseudo random number generation • Simple cipher
hashing Scenario: Map a small number of data values from a large domain 0, 1, … , 𝑁 − 1 into a small set of locations 0,1, … , 𝑜 − 1 so one can quickly check if some value is present.
hashing Scenario: Map a small number of data values from a large domain 0, 1, … , 𝑁 − 1 into a small set of locations 0,1, … , 𝑜 − 1 so one can quickly check if some value is present • hash 𝑦 = 𝑦 mod 𝑞 for 𝑞 a prime close to 𝑜 – or hash 𝑦 = (𝑏𝑦 + 𝑐) mod 𝑞 • Depends on all of the bits of the data – helps avoid collisions due to similar values – need to manage them if they occur
pseudo-random number generation Linear Congruential method: 𝑦 𝑜+1 = 𝑏 𝑦 𝑜 + 𝑑 mod 𝑛 Choose random 𝑦 0 , 𝑏 , 𝑑 , 𝑛 and produce a long sequence of 𝑦 𝑜 ’s [good for some applications, really bad for many others]
simple ciphers • Caesar cipher , A = 1, B = 2, . . . – HELLO WORLD • Shift cipher – 𝑔 (p) = (p + k) mod 26 – 𝑔 −1 (p) = (p – k) mod 26 • More general – 𝑔 −1 (p) = (ap + b) mod 26
modular exponentiation mod 7 1 2 3 4 5 6 a a 1 a 2 a 3 a 4 a 5 a 6 X 1 1 2 2 3 3 4 4 5 5 6 6
modular exponentiation mod 7 1 2 3 4 5 6 a a 1 a 2 a 3 a 4 a 5 a 6 X 1 1 2 3 4 5 6 1 2 2 4 6 1 3 5 2 3 3 6 2 5 1 4 3 4 4 1 5 2 6 3 4 5 5 3 1 6 4 2 5 6 6 5 4 3 2 1 6
modular exponentiation mod 7 1 2 3 4 5 6 a a 1 a 2 a 3 a 4 a 5 a 6 X 1 1 2 3 4 5 6 1 1 1 1 1 1 1 2 2 4 6 1 3 5 2 2 4 1 2 4 1 3 3 6 2 5 1 4 3 3 2 6 4 5 1 4 4 1 5 2 6 3 4 4 2 1 4 2 1 5 5 3 1 6 4 2 5 5 4 6 2 3 1 6 6 5 4 3 2 1 6 6 1 6 1 6 1
Recommend
More recommend