Definition
If m and n are integers, not both zero, the greatest common divisor of m and
n, denoted gcd(m,n), is the largest integer that divides both m and n
Example: gcd(24,32) = 8
Euclids Algorithm:
Input:
Output:
If n == 0
d = |m|
else
while n 0
c=n
n = m mod n
m=c
d=m
return d
gcd(24,32)
= gcd(32,24)
= gcd(24,32 mod 24)
= gcd(24,8)
= gcd(8, 24 mod 8)
= gcd(8,0)
=8
One of the special properties of the greatest common divisor of two numbers is
that it can be written as an integer linear combination of the numbers
Example 2: gcd(32,24) = 8
32 = 124 + 8
Example 3: gcd(54,42)
54 = 142 + 12
42 = 312 + 6
12 = 26 + 0, so 6 = gcd(54,42)
Using back-substitution:
6 = 142 - 312 and 12 = 154 - 142
Thus 6 = 142 - 3(154 - 142 ) = 442 + (-3)54
Apply the Division Algorithm to find the quotient q and remainder x mod n when
dividing x by n
Thus
1 = xa + yn
= ((x mod n)a + qna) + yn
= (x mod n)a + (qa + y)n
Suppose p is prime
We now want an algorithm that computes not only the gcd(a,b) but also the
coefficents x and y so that gcd(a,b) = xa + ybn
This would be particularly useful for computing the decryption function for an affine
cipher
The idea is to compute a sequence of remainders r0, r1, r2, terminating with
the greatest common divisor of the two numbers a and b.
Actually, r2 will be our first true remainder: we start with r0= a and r1 = b.
How do we get the coefficients xi and yi ? From the integer division theorem:
In general, ri+1 = ri-1 mod ri, xi+1 = xi-1 - ri-1/ ri xi, yi+1 = yi-1 - ri-1/ ri yi
r0= a , r1 = b, x0 = 1, y0 = 0; x1 = 0, y1 = 1 (initialization)
i=1
Do the following while ri 0:
ri+1 = ri-1 mod ri, xi+1 = xi-1 - ri-1/ ri xi,
i = i+1
return ri-1
1 d i 1 xi 1
d i 1
d i d i xi
yi 1
yi
di
d i 1
d
d
i 1 d i i
di
d i 1
xi
xi 1
xi
d i 1
xi 1
xi
di
yi
yi 1
yi
d i 1
yi 1
yi
di
d0
d
1
y0
y1 b
x0
x1
1
0
0
1
while d1 0 do:
d0
x0
d1
x1
d = d0, x = x0, y = y0
return d, x, y
y0
y1 1
d0
d i 1
d1
di
1
x0
y0
x1
y1
1 52
52
96 96
1 96
96
52 52
96
1 52
1 52
0 44
Example 4:
a = 52, b = 96
0 44 1 1
1 52 1
52
2 1
44 44 1 1 8
2 1
1 44 1 1 8
44
2 1 4 11 6
8 8
1 8
8
4 4
1 4
11 6 0
2
gcd(52,96) = 4
4 = (-11) 52 + 6 96
11
24
13
d0
d
1
x0 a
x1 b
1
0
while d1 0 do:
d0
d1
x0
1
x1
if d0 1 return No inverse
else
return x0
d0
d i 1
d
d
i 1
1
x0
x1
13
13
1
1
1 13 1
4
13
1
4
4 4
4
1 4
4
1 1 13
43
1 43 1
43
13 13 1
13
13
56
Definition
The least common multiple of positive integers a and b is the least integer
divisible by both a and b.
Notation: lcm(a,b)
Lemma
If a = p i1 p i 2 p ik and b = p j1 p j 2 p ,jk where the pts are distinct primes and
1 2
k
1
2
k
each exponent is nonnegative, then
lcm(a,b) =
Theorem
If a and b are positive integers, then lcm(a,b) =
ab
gcd(a, b)
Modular Arithmetic
Definition
Given integers a, b and m > 0, we say a is congruent to b mod m,
written a b (mod m) or a m b, if and only if a mod m = b mod m.
Theorem
If a, b, m are integers with m > 0, then a m b if and only if m | (b-a)
Definition
A set C of integers is a complete residue system modulo m iff
integer a
c C such that a m c
c, d C, if c m d then c = d
a m a
(ii)
a m b b m a
(iii)
a m b and b m c a m c
(iv)
a m b a +c m b +c and ac m bc
(v)
a m b a d m bd
(vi)
a m and ab m ac b m c
Homework
1. Find d = gcd(43,56) and integers x and y such that d = 43x + 56y, using the
extended Euclidean algorithm
2. For this problem, suppose we are using an affine cipher with encryption function
y = 9x + 4
(a) Encrypt the message nothard; your answer should be a string of capital
letters
(b) Decrypt the message TAAOEKM; your answer should be a string of lower
case letters