Professional Documents
Culture Documents
• Division
– Definition 1:
CSE 504, Chapter 2 (Part 2): The Fundamentals: Algorithms, the Integers & Matrices
– Example : 2
4 | 12 4 18
CSE 504, Chapter 2 (Part 2): The Fundamentals: Algorithms, the Integers & Matrices
3
– Theorem 1:
CSE 504, Chapter 2 (Part 2): The Fundamentals: Algorithms, the Integers & Matrices
5
• Primes
– Definition 2:
CSE 504, Chapter 2 (Part 2): The Fundamentals: Algorithms, the Integers & Matrices
6
• The Division Algorithm
– Theorem 6:
CSE 504, Chapter 2 (Part 2): The Fundamentals: Algorithms, the Integers & Matrices
7
– Definition 3:
Quotient Remainder
= 101 div 11 = 2 = 101 mod 11
CSE 504, Chapter 2 (Part 2): The Fundamentals: Algorithms, the Integers & Matrices
8
• Greatest Common Divisors & Least Common
Multiples
– Definition 4
CSE 504, Chapter 2 (Part 2): The Fundamentals: Algorithms, the Integers & Matrices
9
– Definition 5:
CSE 504, Chapter 2 (Part 2): The Fundamentals: Algorithms, the Integers & Matrices
10
Finding GCD
Using the Theorem: Given integers a>0, b, q, r, such
that b = aq + r, then gcd(b, a) = gcd(a, r).
Euclidian Algorithm
Find gcd (b, a)
while a 0 do
r b mod a
ba
ar
return b
CSE 504, Chapter 2 (Part 2): The Fundamentals: Algorithms, the Integers & Matrices
11
143 = 1 110 + 33
110 = 3 33 + 11
33 = 3 11 + 0
– Theorem 8
CSE 504, Chapter 2 (Part 2): The Fundamentals: Algorithms, the Integers & Matrices
15
3. Cryptology (Caesar Cepher)
a) Encryption:
• Mathematical expression:
f(p) = (p + 3) mod 26 0 p 25
CSE 504, Chapter 2 (Part 2): The Fundamentals: Algorithms, the Integers & Matrices
– Example: What is the secret message produced from 16
the message “Meet you in the park”
Solution:
1. Replace letters with numbers:
meet = 12 4 4 19
you = 24 14 20
in = 8 1 3
the = 19 7 4
park = 15 0 17 10
2. Replace each of these numbers p by f(p) = (p + 3) mod 26
meet = 15 7 7 22
you = 1 17 23
in = 11 16
the = 22 10 7
park = 18 3 20 13
3. Translate back into letters: “PHHW BRX LQ WKH SDUN”
CSE 504, Chapter 2 (Part 2): The Fundamentals: Algorithms, the Integers & Matrices
17
b) Decryption (Deciphering)
CSE 504, Chapter 2 (Part 2): The Fundamentals: Algorithms, the Integers & Matrices
Sp
Modulo Operation ri
ng
Definition: 20
12
a mod n r q, s.t. a q n r /T
where 0 r n 1 op
ic
Example:
6
7 mod 3 = 1
-7 mod 3 = 2
18 CS555
Congruence Relation
Definition: Let a, b, n be integers with n>0, we
say that a b (mod n),
if a – b is a multiple of n.
Properties: a b (mod n)
if and only if n | (a – b)
if and only if n | (b – a)
if and only if a = b+kn for some integer k
if and only if b = a+kn for some integer k
E.g., 327 (mod 5), -1237 (mod 7),
1717 (mod 13)
19
Sp
Properties of the Congruence Relation ri
ng
Proposition: Let a, b, c, n be integers with n>0 20
12
1. a 0 (mod n) if and only if n | a /T
2. a a (mod n) op
ic
3. a b (mod n) if and only if b a (mod n) 6
4. if a b and b c (mod n), then a c (mod n)
Corollary: Congruence modulo n is an
equivalence relation.
Every integer is congruent to exactly one number
in {0, 1, 2, …, n–1} modulo n
20 CS555
Sp
More Properties of the Congruence ri
ng
Relation 20
Proposition: Let a, b, c, n be integers with n>0 12
/T
If a b (mod n) and c d (mod n), then:
op
a + c b + d (mod n), ic
6
a – c b – d (mod n),
a·c b·d (mod n)
E.g., 5 12 (mod 7) and 3 -4 (mod 7), then, …
21 CS555
Sp
Towards Extended Euclidian ri
ng
Algorithm 20
12
/T
Theorem: Given integers a, b > 0, then d = op
gcd(a,b) is the least positive integer that can be ic
represented as ax + by, x, y integer numbers. 6
22 CS555
Sp
The Extended Euclidian Algorithm ri
ng
20
First computes Then computes And 12
/T
b = q 1a + r 1 x0 = 0 y0 = 1
op
a = q2r1 + r2 x1 = 1 y1 = 0 ic
r1 = q3r2 + r3 x2 = -q1x1+x0 6
y2 = -q1y1+y0
rk-3 =qk-1rk-2+rk-1 xk = -qk-1xk-1+xk-2 yk = -qk-1yk-1+yk-2
rk-2 = qkrk-1
We have axk + byk = rk-1 = gcd(a,b)
23 CS555
Sp
Extended Euclidian Algorithm ri
ng
20
Extended_Euclidian (a,b) 12
x=1; y=0; d=a; r=0; s=1; t=b; Invariants: /T
while (t>0) { ax + by = d op
q = d/t; ar + bs = t ic
u=x-qr; v=y-qs; w=d-qt; 6
x=r; y=s; d=t;
r=u; s=v; t=w;
}
return (d, x, y)
end
24 CS555
Sp
Another Way ri
ng
20
12
Find gcd(143, 111)
/T
32 = 143 1 111
143 = 1 111 + 32 op
15 = 111 3 32 ic
111 = 3 32 + 15
= 4111 3 143 6
32 = 2 15 + 2
2 = 32 2 15
15 = 7 2 + 1
= 7 143 9 111
1 = 15 - 7 2
gcd (143, 111) = 1 = 67 111 – 52 143
25 CS555
Properties of Modular Arithmetic
7
To find (11 mod13) do the followings
11 121 4(mod13)
2
11 (11 ) 4 3(mod13)
4 2 2 2
Z n {0,1,..., (n 1)}
This set is referred to as the set of residues, or
residue classes (mod n). That is, each
integer in Zn represents a residue class.
Properties of Modular Arithmetic
Property Expression
Cummitative Laws (w + x) mod n = (x + w) mod n
(w x x) mod n = (x x w) mod n
Associative Laws [(w + x) + y] mod n = [w + (x + y)] mod n
[(w x x) x y] mod n = [w x (x x y)] mod n
Distributive Law [w x (x + y)] mod n = [(w x x) + (w x y)] mod n
Identities (0 + w) mod n = w mod n
(1 x w) mod n = w mod n
Additive Inverse (-w) For each w Zn, there exists a z such that w + z ≡ 0 mod n
Modular Arithmetic
• Finding Inverses in Zn
– The numbers that have inverses in Zn are
relatively prime to n
– We can use the Euclidean Algorithm to see if a
given “x” is relatively prime to “n”; then we
know that an inverse does exist.
– How can we find the inverse without looking at
all the remainders? A problem for large n.
Modular Arithmetic
• Finding Inverses in Zn
– The numbers that have inverses in Zn are
relatively prime to n
– We can use the Euclidean Algorithm to see if a
given “x” is relatively prime to “n”; then we
know that an inverse does exist.
– How can we find the inverse without looking at
all the remainders? A problem for large n.
Modular Arithmetic
• Finding Inverses in Zn
– What is the inverse of 15 in mod 26?
– First use the Euclidean Algorithm to determine
if 15 and 26 are relatively prime
• 26 = 1 * 15 + 11
• 15 = 1 * 11 + 4
• 11 =2*4 +3 Then gcd (26, 15) = 1
• 4 = 1*3 +1
• 3 =3*1 +0
Modular Arithmetic
• Finding Inverses in Zn
– What is the inverse of 15 in mod 26? Now we
now they are relatively prime – so an inverse
must exist.
– We can use the algorithm to work backward to
create 1 (the gcd(26, 15)) as a linear
combination of 26 and 15:
• 1 = x * 26 + y * 15
– Why would we want to do this?
Modular Arithmetic
• Finding Inverses in Zn
– Convert 1 = x * 26 + y * 15 to mod 26 and we
get:
– 1 mod 26 (y * 15) mod 26
– Then if we find y we find the inverse of 15 in
mod 26.
– So we start from 1 and work backward…
Modular Arithmetic
• 26 = 1 * 15 + 11 => 11 = 26 – (1*15)
• 15 = 1 * 11 + 4 => 4 = 15 – (1*11)
• 11 =2*4 + 3 => 3 = 11 – (2*4)
• 4 = 1*3 +1 => 1 = 4 – (1*3)
Step 1) 1 = 4 – (1 * 3) = 4 – 3
Step 2) 1 = 4 – (11 – (2 * 4)) = 3 * 4 - 11
Step 3) 1 = 3 * (15 – 11) – 11 = 3 * 15 – 4 * 11
Step 4) 1 = 3 * 15 – 4(26 – (1*15)
Step 5 ) 1 = 7 * 15 – 4 * 26 = 105 – 104 >>check
Modular Arithmetic
• Finding Inverses in Zn
– So, what is the inverse of 15 in mod 26?
– 1 = 7 * 15 – 4 * 26 converts to
– 1 7 * 15 mod 26
– 7 is the inverse of 15 in mod 26
– Can you use the same result to show that 11 is
its own inverse in mod 15?