CSE 20: Discrete Mathematics for Computer Science Prof. Shachar Lovett
Today’s Topics: 1. Modular arithmetics 2
1. Modular Arithmetic 3
Modular arithmetics 4
Residue classes Remember that equivalence relations = partition of universe to equivalence classes Even = [0 mod 2] = {…,-4,-2,0,0,2,4,…} Odd = [1 mod 2] = {…,-3,-1,1,3,…} Partition by value modulo 2 5
Residue classes Similarly, there are 3 residue classes modulo 3 [0 mod 3] = {x: x=3a} = {…,-6,-3,0,3,6,…} [1 mod 3] = {x: x=3a+1} = {…,-5,-2,1,4,7,…} [2 mod 3] = {x: x=3a+2} = {…,-4,-1,2,5,8,…} Similarly, modulo n defines n residue classes. For x Z, we denote by [x mod n] its residue class. 6
Modular addition We can add residue classes. Fix n. [x mod n]+[y mod n]=[x+y mod n] This is well defined. What does that mean? A. For any x,y, the sum x+y is defined B. The definition of sum does not depend on choice of representatives C. There is a way to obtain all residue classes D. I don’t know 7
Modular addition Residue classes mod mod 5 1 mod 5 2 mod 53 mod 5 4 mod 5
Modular addition Residue classes mod 5 [x mod 5]+[1 mod 5]=[x+1 mod 5] 9 0 mod 5 1 mod 5 2 mod 53 mod 5 4 mod 5
Modular addition [x mod n]+[y mod n]=[x+y mod n] Theorem: this is well defined This means: if x,x’ Z are such that x mod n=x’ mod n; and y,y’ Z are such that y mod n=y’ mod n. Then (x+y) mod n = (x’+y’) mod n. So, the definition of addition does not depend on the chosen representatives for each residue class 10
Modular addition [x mod n]+[y mod n]=[x+y mod n] Theorem: this is well defined Proof (direct proof): If x mod n=x’ mod n then x’=x+na for a Z If y mod n=y’ mod n then y’=y+nb for b Z Then (x’+y’)=(x+y)+n(a+b) So [x’+y’ mod n]=[x+y mod n] QED 11
Modular addition [2 mod 5]+[4 mod 5]= A. 0 B. [2 mod 5] C. [1 mod 5] D. [1 mod 4] E. None/other/more than one 12
Modular multiplication [x mod n]*[y mod n]=[x*y mod n] Theorem: this is well defined Proof (direct proof): If x mod n=x’ mod n then x’=x+na for a Z If y mod n=y’ mod n then y’=y+nb for b Z Then (x’*y’)=(x*y)+n(ay+bx+nab) So [x’*y’ mod n]=[x*y mod n] QED 13
Modular multiplication [2 mod 5]*[4 mod 5]= A. [1 mod 5] B. [2 mod 5] C. [3 mod 5] D. [4 mod 5] E. None/other/more than one 14
Modular multiplication [2 mod 5]*[4 mod 7]= A. [1 mod 5] B. [2 mod 7] C. [3 mod 35] D. [8 mod 35] E. None/other/more than one 15
Modular multiplication X={0,1,2,3,4} Define: xRy if [2x mod 5]=[y mod 5] Is R… A. Symmetric B. Reflexive C. Transitive D. More than one E. None 16
The ring Z n Define Z n ={[0 mod n],[1 mod n],…,[n-1 mod n]} The set of all residue classes modulo n We defined addition and multiplication on Z n You can check: they obey all the usual definitions of addition and multiplication (abbrv. [x]=[x mod n]) [x]+[y]=[y]+[x]; [x]*[y]=[y]*[x] [x]*([y]+[z])=[x]*[y]+[x]*[z] [x]+[0]=[x]; [x]*[1]=[x] … Sets which support addition and multiplication are called rings 17
The ring Z n If n is not prime, weird stuff can happen Example: [2 mod 6] * [3 mod 6] = A. [0 mod 6] B. [2 mod 6] C. [3 mod 6] D. [5 mod 6] E. None/other/more than one 18
The ring Z n If n is not prime, weird stuff can happen Example: [2 mod 6] * [3 mod 6] = [0 mod 6] That is, we multiplied two nonzero elements, and got zero as a result (these are called zero divisors) 19
Applications of modular arithmetic