You are on page 1of 17

Euclidean and Extended Euclidean Algorithm

THE EUCLIDEAN ALGORITHM


• It is a simple procedure for determining the greatest common
divisor of two positive integers.
• The greatest common divisor of a and b is the largest
integer that divides both a and b.
Cont…
• More formally, the positive integer c is said to be the greatest
common divisor of a and b if
1. c is a divisor of a and of b.
2. any divisor of a and b is a divisor of c.
• For example,
GCD (30, 45) = 15
• Because we require that the greatest common divisor be
positive, gcd(a, b) = gcd(a, -b) = gcd(-a, b) = gcd(-a, -b).
• For example,
GCD (60, 24) = GCD (60, -24) = 12
Find GCD (710, 310) using Euclidean algorithm
a = 710, b = 310
Formulae Example
a≥b>0
• a = q1b + r1 0 ≤ r1 < b 710 = 2 X 310 + 90
• b = q2r1 + r2 0 ≤ r2 < r1 310 = 3 X 90 + 40
• r1 = q3r2 + r3 0 ≤ r3 < r2 90 = 2 X 40 + 10
• r2 = q4r3 + r4 0 ≤ r4 < r3 40 = 4 X 10 + 0
.
.
• rn−1 = qn+1 rn + 0
• d = GCD (a, b) = rn d=GCD (710, 310) = 10
Last non-zero reminder is GCD
Quick Understanding

GCD is 2
Find GCD (1180, 482) using Euclidean algorithm

q a b r
2 1180 482 216
2 482 216 50
4 216 50 16
3 50 16 2
8 16 2 0

GCD is 2
Relative Primes

• Two integers are relatively prime if and only if their only


common positive integer factor is 1.
 GCD (a, b) = 1
 GCD (8, 15) = 1
 15 = 1 x 8 + 7
8 = 1 x 7 + 1
7 = 7 X 1 + 0
Special points

• GCD (a, 0)=a


• GCD (0, b)=b
Exercise
1. Find GCD (2740, 1760) using Euclidean Algorithm.
2. Find GCD (25, 60) using Euclidean Algorithm. (Assume that
first number should be smaller than second number in the
initial step)
Answers
1. GCD (2740, 1760) = 20
2. GCD (25, 60) = 5
Extended Euclidean Algorithm
• Given two integers a & b, we often need to
find other two integers, s & t such that,
 s x a + t x b = GCD (a, b)
Difference between Euclidean and EEA
Euclidean EEA
Calculating the GCD (a, b) Calculating the GCD (a, b)
and two more variables s & t

Only one set of calculation 3 sets of calculations


Example 1: Find GCD of (161, 28) using EEA and
also find s & t
q a b r s1 s2 s t1 t2 t
5 161 28 21 1 0 1 0 1 -5
1 28 21 7 0 1 -1 1 -5 6
3 21 7 0 1 -1 4 -5 6 -23

5 Initial Assumptions are s1 = 1, s2 = 0


28 161 But, t1 = 0, t2 = 1
140
21 s = s1 – s2q, similarly, t = t1 – t2q,
s = 1 – 0 (5) t = 0 – 1 (5)
s=1 t = -5

So, GCD (161, 28) = 7 and S = -1 and t = 6


Testing of Answer
• s x a + t x b = GCD (a, b)
• (-1 x 161) + (6 x 28) = 7
• -161+168 = 7
• 7=7
• Proved
Exercise
• Find GCD (4864, 3458), s & t
Answer

• GCD (4864, 3458) = 38, s = 32 & t = -45

You might also like