Euclidian Algorithm
Euclidian Algorithm
THEORY OF NUMBERS
EUCLIDEAN ALGORITHM
Prepared by:
ALFRED M. OROSCO, M.B.M.
.
Euclidian Algorithm
The Quotient Remainder Theorem
When we want to prove some properties about modular
arithmetic we often make use of the quotient remainder theorem.
It is a simple idea that comes directly from long division.
The quotient remainder theorem says:
Given any integer a, and a positive integer b, there
exist unique integers q and r such that
a=b*q+r where 0≤r<b
We can see that this comes directly from long division. When
we divide a by b in long division, q is the quotient and r is the
remainder.
If we can write a number in this form then a mod b = r.
.
Euclidian Algorithm
Examples:
1. a = 7, b = 2 2. a = 8, b = 4
7=2*3+1 8=4*2+0
7 mod 2 = 1 8 mod 4 = 0
3. a = 13, b = 5 4. a = 16, b = 26
13 = 5 * 2 + 3 16 = 26 * 1 + 10
Euclidian Algorithm
Modular Exponentiation Property
ab mod c = ( (a mod c)b ) mod c
Euclidian Algorithm
Calculating ab mod c quickly if b is a power of 2
Using modular multiplication rules:
a2 mod c = (a * a) mod c = ((a mod c) * (a mod c)) mod c
Example 2 Calculate 764 mod 13.
Solution: 71 mod 13 = 7
72 mod 13 = (71 * 71) mod 13 = (71 mod 13 * 71 mod 13) mod
13
We can substitute our previous result for 71 mod 13 into this
equation.
72 mod 13 = (7 * 7) mod 13 = 49 mod 13 = 10
72 mod 13 = 10
74 mod 13 = (72 * 72) mod 13 = (72 mod 13 * 72 mod 13)
mod 13
.
Euclidian Algorithm
We can substitute our previous result for 74 mod 13 into this
equation.
78 mod 13 = (9 * 9) mod 13 = 81 mod 13 = 3
78 mod 13 = 3
We continue in this manner, substituting previous results into
our equations. After 3 iterations we hit:
764 mod 13 = (732 * 732) mod 13 = (732 mod 13 * 732
mod 13) mod 13
764 mod 13 = (3 * 3) mod 13 = 9 mod 13 = 9
764 mod 13 = 9
.
Euclidian Algorithm
Calculating ab mod c for any b (Repeated Square
Algorithm)
Example 3 What is 57 mod 21?
Solution: Step 1: Write the exponent as a sum of powers of 2.
We first write the exponent 7 in binary: 7 10 = 1112
Recall that binary just defines a sum of powers of 2,
so: 7 = 2 2 + 21 + 20
In other words: 7 = 4 + 2 + 1
We can now rewrite the question as:
What is 57 mod 21 = 54+2+1 mod 21 ?
= (54)(52)(51) mod 21 ?
.
Euclidian Algorithm
Calculating ab mod c for any b (Repeated Square
Algorithm)
(cont…) Step 2: Construct a table of repeated squares.
51 = 5 mod 21
52 = (5)2 = 25 = 4 mod 21
54 = (52)2 = (4)2 = 16 mod 21
Notice that each line is the square of the previous line
(reduced mod 21).
Step 3: Combine the results of Steps 1 and 2.
57 mod 21 = (54)(52)(51) mod 21
57 mod 21 = (16)(4)(5) mod 21
57 mod 21 = 320 mod 21
7 7
.
Euclidian Algorithm
The Euclidian Algorithm
The greatest common divisor (gcd) of two integers a and b is
the largest integer that divides both a and b. The gcd of two
integers can, of course, be found by listing all their positive divisors
and choosing the largest one common to each; but this is
inconvenient for large numbers. A more efficient process, involving
repeated application of the Division Algorithm, is given in the
seventh Book of the Elements. Although there is historical evidence
that this method predates Euclid, today it is referred to as the
Euclidean Algorithm.
The Euclidean Algorithm may be described as follows: Let a and b
be two integers whose greatest common divisor is desired. Because
gcd(|a|,|b|) = gcd(a , b), there is no harm in assuming that a b >
0. The first step is to apply the Division Algorithm to a and b to get
a = q1 b + r1 0 r1 < b
.
Euclidean Algorithm
If it happens that r1 = 0, then b|a and gcd(a, b) = b. When r 1 ≠ 0,
divide b by r1 to produce integers q2 and r2 satisfying
b = q 2 r1 + r 2 0 r2 < r 1
If r2 = 0, then we stop; otherwise, proceed as before to obtain
r1 = q 3 r2 + r 3 0 r3 < r 2
This division process continues until some zero remainder
appears, say, at the (n + l)th stage where rn-1 is divided by rn (a
zero remainder occurs sooner or later because the decreasing
sequence b > r1 > r2 > > 0 cannot contain more than b
integers).
.
Euclidean Algorithm
The result is the following system of equations:
b = q 2 r1 + r 2 0 < r2 < r1
r1 = q 3 r2 + r 3 0 < r3 < r2
rn-2 = qnrn-1 + rn 0 < rn < rn-1
rn-1 = qn+1rn + 0
We argue that rn, the last nonzero remainder that appears in this
manner, is equal to gcd(a , b). Our proof is based on the lemma as
follows.
.
Euclidean Algorithm
Lemma. If a= qb + r, then gcd(a, b)= gcd(b, r).
Proof. If d = gcd(a, b), then the relations d|a and d|b together
imply that d|(a qb), or d|r. Thus, d is a common divisor of both b
and r. On the other hand, if c is an arbitrary common divisor of b
and r, then c|(qb + r), whence c|a. This makes c a common
divisor of a and b, so that c d. It now follows from the definition
of gcd(b, r) that d = gcd(b, r).
Using the result of this lemma, we simply work down the above
system of equations, obtaining
Euclidean Algorithm
Example 1 Find the gcd of 270 and 192.
Solution: Given a = 270 and b = 192 (a ≠ 0, b ≠ 0), we can
write
270 = 192 * 1 + 78
192 = 78 * 2 + 36
78 = 36 * 2 + 6
36 = 6 * 6 + 0
Having reached a remainder of 0, then we say that the
gcd (270, 192) = 6.
.
Euclidean Algorithm
Example 2 What is the gcd of 960 and 432?
Solution: Given a = 960 and b = 432 (a ≠ 0, b ≠ 0), we can
write
960 = 432 * 2 + 96
432 = 96 * 4 + 48
96 = 48 * 2 + 0
Since the remainder is 0, then we say that the
gcd (960, 432) is 48.
.
Euclidean Algorithm
Extended Euclidean Algorithm
Suppose we wish to compute gcd(27, 33). First, we divide the
bigger one by the smaller one: 33 = 27 * 1 + 6, thus, gcd(33, 27)
= gcd(27, 6). Repeating this trick: 27 = 6 * 4 + 3 and we
see gcd(27, 6) = gcd(6, 3). Lastly, 6 = 3 * 2 + 0.
Since 6 is a perfect multiple of 3, gcd(6, 3) = 3, and we have
found that gcd(33, 27) = 3.
This algorithm does not require factorizing numbers, and is fast.
We obtain a crude bound for the number of steps required by
observing that if we divide a by b to get a = bq + r, and r > ,
then in the next step we get a remainder r′ ≤ . Thus every two
steps, the numbers shrink by at least one bit.
.
Euclidean Algorithm
The above equations actually reveal more than the gcd of two
numbers. We can use them to find integers a, b such that
3 = 33a + 27b
First rearrange all the equations so that the remainders are the
subjects:
6 = 33 − 1 * 27
3 = 27 − 4 * 6
Then we start from the last equation, and substitute the next
equation into it:
3 = 27 − 4 * (33 − 1 * 27) = (−4) * 33 + 5 * 27)
Hence, a = −4, b = 5.
.
Euclidean Algorithm
Example 3 Given that gcd(56, 72) = 56a + 72b, use the
Euclidean Algorithm to find a and b.
Solution: 72 = 56 * 1 + 16 8 = 56 – (16 * 3)
56 = 16 * 3 + 8 = 56 – (72 – 56) * 3
16 = 8 * 2 + 0 = 4 * 56 – 3 * 72
= 4(56) + (–3)72
Hence, a = 4, b = 3.
.
Euclidean Algorithm
Example 4 Using the Euclidean Algorithm, find a and b to satisfy
that gcd(24, 138) = 24a + 138b.
Solution: 138 = 24 * 5 + 18 6 = 24 – (6 * 3)
24 = 18 * 1 + 6 = 24 – (18)
18 = 6 * 3 + 0 = 24 – (138 – 24 * 5)
= 6 * 24 – 138
= (6)24 + (–1)138
Hence, a = 6, b = –1.