You are on page 1of 41

1

• Division

– Definition 1:

If a and b are integers with a  0, we say that a


divides b if there is an integer c such that b = ac.
When a divides b we say that a is a factor of b and
that b is multiple of a. The notation a | b denotes that
a divides b. We write a b when a does not divide b

CSE 504, Chapter 2 (Part 2): The Fundamentals: Algorithms, the Integers & Matrices
– Example : 2

4 | 12 4 18

3 7, since 7 | 3 is not an integer.

If a  0, then a|0 and a|a. Also, 1|b for every b

CSE 504, Chapter 2 (Part 2): The Fundamentals: Algorithms, the Integers & Matrices
3
– Theorem 1:

Let a, b and c be integers. Then


1. If a|b and a|c, then a | (b + c);
2. If a|b, then a|bc c  Z;
3. If a|b and b|c, then a|c

Proof: Suppose a|b and a|c   s  Z,  t  Z such


that: b = as and c = at. Therefore:
b + c = as + at = a (s + t); which means that
a | (b + c). This establishes part 1 of the theorem.
Parts 2 and 3 are left to you! Q.E.D.
CSE 504, Chapter 2 (Part 2): The Fundamentals: Algorithms, the Integers & Matrices
4

Corollary 1: if a, b and c are integers such that a|b and


a|c, then a | mb +nc whenever m and n are integers.

Proof: Part 2 of theorem 1 shows that: a | mb and a | nc


whenever m and n are integers. Using part 1 of the
theorem  a | mb + nc. Q.E.D.

CSE 504, Chapter 2 (Part 2): The Fundamentals: Algorithms, the Integers & Matrices
5
• Primes

– Definition 2:

A positive integer p greater than 1 is called prime if


the only positive factors of p are 1 and p. A positive
integer that is greater than 1 and is not prime is
called composite.

– Example: 7 is prime, 9 is composite.

CSE 504, Chapter 2 (Part 2): The Fundamentals: Algorithms, the Integers & Matrices
6
• The Division Algorithm

– Theorem 6:

The Division Algorithm


Let a be an integer and d a positive integer. Then !
(q, r)  Z2; 0  r < d: a = dq +r.

CSE 504, Chapter 2 (Part 2): The Fundamentals: Algorithms, the Integers & Matrices
7
– Definition 3:

d is called the divisor, a the dividend, q the quotient


and r the remainder.
q = a div d, r = a mod d.

– Example: 101 = 11.9 + 2

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

Let a and b be integers, not both zero. The largest


integer d such that d|a and d|b is called the greatest
common divisor of a and b. It is denoted gcd (a, b).

Example: gcd (24, 36)


Div (24) = {1,2,3,4,6,8,12,24}
Div (36) = {1,2,3,4,6,8,9,12,18,36}
Com(24,36) = = {1,2,3,4,6,12}
gcd(24,36) = 12

CSE 504, Chapter 2 (Part 2): The Fundamentals: Algorithms, the Integers & Matrices
9
– Definition 5:

The integers a and b are relatively prime (rp) if


gcd(a, b) =1.

Example: 17 and 22 are rp since gcd(17,22) = 1.

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
ba
ar
return b
CSE 504, Chapter 2 (Part 2): The Fundamentals: Algorithms, the Integers & Matrices
11

Euclidian Algorithm Example


Find gcd(143, 110)

143 = 1  110 + 33
110 = 3  33 + 11
33 = 3  11 + 0

gcd (143, 110) = 11


Another Example
Compute (346, 592).

592 = 346 · 1 + 246


346 = 246 · 1 + 100
246 = 100 · 2 + 46
100 = 46 · 2 + 8
46 = 8 · 5 + 6
8 = 6 · 1 + 2
6 = 2 · 3 + 0

So (346, 592) = 2.


13
• Modular Arithmetic
– Definition 8:

Let (a, b)  Z2,, m  Z+ then a is a congruent to b


modulo m if m divides a –b.
Notation: a  b (mod m).

– Theorem 8

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.
CSE 504, Chapter 2 (Part 2): The Fundamentals: Algorithms, the Integers & Matrices
14
– Example: 17  5 (mod 6)
24  14 (mod 6)?
Since: 6|(17 – 5) = 12  17  5 (mod 6)
6 does not divide 10
 24 is not congruent to 14 (mod 6)
– Theorem 9:

Let m be a positive integer. The integers a and b are


congruent modulo m if and only if
k  Z; a = b + km

CSE 504, Chapter 2 (Part 2): The Fundamentals: Algorithms, the Integers & Matrices
15
3. Cryptology (Caesar Cepher)

a) Encryption:

• Making messages secrets by shifting each letter


three letters forward in the alphabet
BE XA

• 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)

f(p) = (p + k) mod 26 (shift cepher)


 f -1(p) = (p – k) mod 26

Caesar’s method and shift cipher are very vulnerable


and thus have low level of security (reason frequency
of occurrence of letters in the message)
 Replace letters with blocks of letters.

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., 327 (mod 5), -1237 (mod 7),
1717 (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

How to find such x and y?

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
= 4111  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

1. [(a mod n) + (b mod n)] mod n = (a + b) mod n


2. [(a mod n) - (b mod n)] mod n = (a - b) mod n
3. [(a mod n) x (b mod n)] mod n = (a x b) mod n
Proof of 1.
Let (a mod n) = Ra and (b mod n) = Rb. Then, we can write
a = Ra + jn for some integer j and b = Rb + kn for some integer k.
(a + b) mod n = (Ra + jn + Rb + kn) mod n
= [Ra + Rb + (k + j) n] mod n
= (Ra + Rb) mod n
= [(a mod n) + (b mod n)] mod n
Examples
11 mod 8 = 3; 15 mod 8 = 7
[(11 mod 8 ) + (15 mod 8)] mod 8 = 10 mod 8 = 2
(11 + 15) mod 8 = 26 mod 8 = 2
[(11 mod 8 ) - (15 mod 8)] mod 8 = -4 mod 8 = 4
(11 - 15) mod 8 = -4 mod 8 = 4
[(11 mod 8 ) x (15 mod 8)] mod 8= 21 mod 8 = 5
(11 x 15) mod 8 = 165 mod 8 = 5
Exponentiation
• Exponentiation is done by repeated
multiplication, as in ordinary arithmetic.

7
To find (11 mod13) do the followings
11 121  4(mod13)
2

11 (11 )  4  3(mod13)
4 2 2 2

11 11 4  3  132  2(mod13)


7
Properties of Modular Arithmetic

Define the set Zn as the set of nonnegative


integers less than n:

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

We can label the residue classes (mod n) as:


[0],[1],[2],...,[n-1], where
[r] = {a: a is an integer, a ≡ r (mod n)}.
E.g.: The residue classes (mod 4) are
[0] = {..., -16,-12,-8,-4,0,4,8,12,16, ...}
[1] = {..., -15,-11,-7,-3,1,5,9,13,17, ...}
[2] = {..., -14,-10,-6,-2,2,6,10,14,18, ...}
[3] = {..., -13,-9,-5,-1,3,7,11,15,19, ...}
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

• A Multiplication Table in Zn: Summary


– The numbers that have inverses in Zn are
relatively prime to n
• That is: gcd(x, n) = 1
– The numbers that do NOT have inverses in Zn
have common prime factors with n
• That is: gcd(x, n) > 1
Modular Arithmetic

• A Multiplication Table in Zn: Summary


– The results have implications for division:
• Some divisions have no answers
– 3 * x = 2 mod 6 has no solutions => 2/3 has no equivalent
in Z6
• Some division have multiple answers
– 2 * 2 = 4 mod 6 => 4/2 = 2 mod 6
– 2 * 5 = 4 mod 6 => 4/2 = 5 mod 6
• Only numbers that are relatively prime to n will be
uniquely divisible by all elements of Zn
Modular Arithmetic

• A Multiplication Table in Zn: Summary


– The results have implications for division:
• Zero divisors exist in some mods:
• 3 * 2 = 0 mod 6 => 0/3 = 2 and 0/2 = 3 in mod 6
• 3 * 6 = 0 mod 9 => 0/3 = 6 and 0/6 = 3 in mod 9
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?

You might also like