slides

advertisement
CSE 20
DISCRETE MATH
Prof. Shachar Lovett
http://cseweb.ucsd.edu/classes/wi15/cse20-a/
Clicker
frequency:
CA
Todays topics
• Modular arithmetic
• Section 6.2 in Jenkyns, Stephenson
Recall: equivalence relations
• Saw two equivalent definitions
• xRy is an equivalence relation if…
• Symmetric: ∀π‘₯, 𝑦, π‘₯𝑅𝑦 ⇔ 𝑦𝑅π‘₯
• Reflexive: ∀π‘₯, π‘₯𝑅π‘₯
• Transitive: ∀π‘₯, 𝑦, 𝑧, π‘₯𝑅𝑦 ∧ 𝑦𝑅𝑧 → π‘₯𝑅𝑧
• Equivalently: there is a partition of the universe to
equivalence classes, and xRy iff x,y are in the same class
• Can be phrased as: ∃𝐢, πΆπ‘™π‘Žπ‘ π‘ : π‘ˆ → 𝐢 such that
π‘₯𝑅𝑦 ⇔ πΆπ‘™π‘Žπ‘ π‘  π‘₯ = πΆπ‘™π‘Žπ‘ π‘ (𝑦)
Today: modular arithmetic
• A very important (and very useful) example of an
equivalence relation
• Recall our example of {even,odd} classes:
• π‘₯𝑅𝑦 ⇔ 2| π‘₯ − 𝑦 ⇔x,y are both even, or both odd
• Modular arithmetic generalizes this from 2 to any divisor
Modular arithmetic: definition
• Fix π‘š ≥ 2
• Define a relation: integers x,y are equivalent modulo m, if
π‘₯ π‘šπ‘œπ‘‘ π‘š = 𝑦 π‘šπ‘œπ‘‘ π‘š
• Equivalently, π‘š|π‘₯ − 𝑦
• Defines a partition of Z to m equivalence classes
π‘Ž π‘š = {π‘₯ ∈ 𝑍: π‘₯ π‘šπ‘œπ‘‘ π‘š = π‘Ž}
where π‘Ž ∈ {0,1, … , π‘š − 1}
Modular arithmetic: examples
• π‘Ž π‘š = {π‘₯ ∈ 𝑍: π‘₯ π‘šπ‘œπ‘‘ π‘š = π‘Ž}
• What is [0]2?
A. All integers
B. 0
C. Even numbers
D. Odd numbers
E. Other
Modular arithmetic: examples
• π‘Ž π‘š = {π‘₯ ∈ 𝑍: π‘₯ π‘šπ‘œπ‘‘ π‘š = π‘Ž}
• What is [1]2?
A. All integers
B. 1
C. Even numbers
D. Odd numbers
E. Other
Modular arithmetic: examples
• π‘Ž π‘š = {π‘₯ ∈ 𝑍: π‘₯ π‘šπ‘œπ‘‘ π‘š = π‘Ž}
• What is [0]3?
A. All integers
B. All integers divisible by 3
C. All integers whose value mod 3 is equal to 1
D. All integers whose value mod 3 is equal to 2
E. Other
Modular arithmetic: examples
• π‘Ž π‘š = {π‘₯ ∈ 𝑍: π‘₯ π‘šπ‘œπ‘‘ π‘š = π‘Ž}
• What is [1]3?
A. All integers
B. All integers divisible by 3
C. All integers whose value mod 3 is equal to 1
D. All integers whose value mod 3 is equal to 2
E. Other
Modular arithmetic: examples
• π‘Ž π‘š = {π‘₯ ∈ 𝑍: π‘₯ π‘šπ‘œπ‘‘ π‘š = π‘Ž}
• What is [4]3?
A. All integers
B. All integers divisible by 3
C. All integers whose value mod 3 is equal to 1
D. All integers whose value mod 3 is equal to 2
E. Other
Modular arithmetic: notation
• Notation: to say that π‘₯ ∈ π‘Ž π‘š , we typically write
π‘₯ ≡ π‘Ž π‘šπ‘œπ‘‘ π‘š
• More generally, for any two integers x,y we can write
π‘₯ ≡ 𝑦 π‘šπ‘œπ‘‘ π‘š
which means that x,y belong to the same equivalence class
modulo m, which means π‘š|π‘₯ − 𝑦.
Modular addition and multiplication
• Why is modular arithmetic so important? Because we can
operate directly on the equivalence classes, adding and
multiplying them (for a fixed modulus m)
• Definition: for any fixed π‘š ≥ 2,
π‘Ž
π‘š
+ 𝑏
π‘š
=
π‘Ž + 𝑏 π‘šπ‘œπ‘‘ π‘š
∗ 𝑏
π‘š
=
π‘Ž ∗ 𝑏 π‘šπ‘œπ‘‘ π‘š
π‘š
And
π‘Ž
π‘š
• Theorem: this is well defined
• What does this even mean???
π‘š
Modular addition
• Theorem: π‘Ž π‘š + 𝑏 π‘š =
π‘Ž + 𝑏 π‘šπ‘œπ‘‘ π‘š
π‘š
• Interpretation: if we take any π‘₯ ∈ π‘Ž π‘š , and any 𝑦 ∈ 𝑏 π‘š ,
then it is always true that π‘₯ + 𝑦 ∈
π‘Ž + 𝑏 π‘šπ‘œπ‘‘ π‘š
π‘š
• Equivalently:
π‘₯ π‘šπ‘œπ‘‘ π‘š + 𝑦 π‘šπ‘œπ‘‘ π‘š
π‘šπ‘œπ‘‘ π‘š = π‘₯ + 𝑦 π‘šπ‘œπ‘‘ π‘š
Modular addition: proof
• Theorem: π‘Ž π‘š + 𝑏 π‘š =
π‘Ž + 𝑏 π‘šπ‘œπ‘‘ π‘š
π‘š
• Proof: we need to show
∀π‘₯ ∈ π‘Ž
π‘š, 𝑦
∈ 𝑏
π‘š,
π‘₯ + 𝑦 π‘šπ‘œπ‘‘ π‘š = π‘Ž + 𝑏 π‘šπ‘œπ‘‘ π‘š
Let π‘₯ = π‘π‘š + π‘Ž, 𝑦 = π‘žπ‘š + 𝑏 and π‘Ž + 𝑏 = π‘Ÿπ‘š + 𝑐, with
π‘Ž, 𝑏, 𝑐 ∈ {0, … , π‘š − 1}. Then:
π‘₯+𝑦 = 𝑝+π‘ž π‘š+ π‘Ž+𝑏 = 𝑝+π‘ž+π‘Ÿ π‘š+𝑐
So π‘₯ + 𝑦 ∈ 𝑐
π‘š,
and also a + 𝑏 ∈ 𝑐
π‘š.
QED.
Modular addition: examples
• What is 0 2 + 1 2 ?
A.
B.
C.
D.
E.
02
12
21
All integers
Other
Modular addition: examples
• What is 4 7 + 5 7 ?
A.
B.
C.
D.
E.
17
27
72
74
Other
Modular addition: examples
• What is 4 7 + 1 2 ?
A.
B.
C.
D.
E.
57
5 14
72
1 14
Other
Be careful!
• We can only add π‘Ž π‘š + 𝑏 𝑛 if 𝑛 = π‘š. Otherwise, this is
not defined!!!
• Also: in the special case of π‘š = 2, addition modulo 2
corresponds to XOR
• 0 2+ 0 2= 0 2
• 0 2+ 1 2= 1 2
• 1 2+ 0 2= 1 2
• 1 2+ 1 2= 0 2
Modular multiplication
• Theorem: π‘Ž π‘š ∗ 𝑏 π‘š =
π‘Ž ∗ 𝑏 π‘šπ‘œπ‘‘ π‘š
π‘š
Modular multiplication: proof
• Theorem: π‘Ž π‘š ∗ 𝑏 π‘š =
π‘Ž ∗ 𝑏 π‘šπ‘œπ‘‘ π‘š
π‘š
• Proof: we need to show
∀π‘₯ ∈ π‘Ž
π‘š, 𝑦
∈ 𝑏
π‘š,
π‘₯ ∗ 𝑦 π‘šπ‘œπ‘‘ π‘š = π‘Ž ∗ 𝑏 π‘šπ‘œπ‘‘ π‘š
Let π‘₯ = π‘π‘š + π‘Ž, 𝑦 = π‘žπ‘š + 𝑏 and π‘Ž ∗ 𝑏 = π‘Ÿπ‘š + 𝑐, with π‘Ž, 𝑏, 𝑐 ∈
{0, … , π‘š − 1}. Then:
π‘₯ ∗ 𝑦 = π‘π‘š + π‘Ž ∗ π‘žπ‘š + 𝑏
= π‘π‘žπ‘š2 + π‘Žπ‘žπ‘š + π‘π‘π‘š + π‘Žπ‘
= π‘π‘žπ‘š2 + π‘Žπ‘žπ‘š + π‘π‘π‘š + π‘Ÿπ‘š + 𝑐
= π‘š π‘π‘žπ‘š + π‘Žπ‘ž + 𝑏𝑝 + π‘Ÿ + 𝑐
So π‘₯ ∗ 𝑦 ∈ 𝑐
π‘š,
and also a ∗ 𝑏 ∈ 𝑐
π‘š.
QED.
Applications of modular arithmetic
• For a fixed π‘š ≥ 2, we can add, multiply and take modulo
m, and the value (modulo m) will stay invariant
• This is very useful; many algorithmic applications
• Example 1: verifying calculations
• Prove that 12294793 ∗ 2329373 ≠ 92739273
• Hint: use values modulo 10
Applications of modular arithmetic
• Example 1: prove that 12294793 ∗ 2329373 ≠ 92739273
• Solution: compute values modulo 10
• 12294793 ≡ 3 π‘šπ‘œπ‘‘ 10
• 2329373 ≡ 3 π‘šπ‘œπ‘‘ 10
• 92739273 ≡ 3 π‘šπ‘œπ‘‘ 10
• But 3 ∗ 3 ≡ 9 π‘šπ‘œπ‘‘ 10 , so the equation cannot be correct
Applications of modular arithmetic
• Example 2: number theory
• Theorem: there are no π‘₯, 𝑦 ∈ 𝑍 such that π‘₯ 2 + 𝑦 2 = 1234567
• Hint: use values modulo 4
Applications of modular arithmetic
• Theorem: there are no π‘₯, 𝑦 ∈ 𝑍 such that π‘₯ 2 + 𝑦 2 =
1234567
• Hint: use values modulo 4
• Solution: for any π‘₯ ∈ 𝑍, π‘₯ 2 ∈ 0 4 or π‘₯ 2 ∈ 1 4 .
• There are 4 cases to check:
•
0
4
∗ 0
4
= 0 4; 1
4
∗ 1
4
= 1 4; 2
4
∗ 2
4
= 0 4; 3
4
∗ 3
4
= 1 4;
• So, π‘₯ 2 + 𝑦 2 π‘šπ‘œπ‘‘π‘’π‘™π‘œ 4 can either be 0 4 , 1 4 or 2 4
• But 1234567 ≡ 3 (π‘šπ‘œπ‘‘ 4), so they cannot be equal!
Applications of modular arithmetic
• Cryptography
• Your online transactions are encrypted, based on modular
arithmetic
• RSA public key crpytosystem:
• Very large m, product of two large primes m=p*q
• Message: π‘₯ ∈ {0,1, … , π‘š − 1}
• Encryption: π‘₯ 𝑒 π‘šπ‘œπ‘‘ π‘š
• Security is based on the assumption that factorization is hard
Download