Euclidean and Extended Euclidean Algorithm
Euclidean and Extended Euclidean Algorithm
Euclidean and Extended Euclidean Algorithm
EXTENDED EUCLIDEAN
ALGORITHM
• GCD(b,0) = b
• GCD(a,a) = a
• GCD(a,b) = GCD(b,a mod b)
12 33
integers
Divisors
Common Divisors
Greatest Common
Divisor
(GCD)
• Understanding GCD Example 1
12 33
integers
Divisors
1, 2, 3, 4, 6,12 1, 3, 11, 33
Common Divisors
1, 3
Greatest Common
Divisor 3
(GCD)
• Euclid’s Algorithm for finding GCD (methode 1):
Find the GCD(12,33)
• Euclid’s Algorithm for finding GCD (methode 1):
Find the GCD(12,33)
Q A B R
2 33 12 9
1 12 9 3
3 9 3 0
3 0
Here a = 33 , b = 12
GCD(a,b) = GCD(b, a mod b)
GCD(33,12) = GCD(12 , 33 mod 12) = GCD(12, 9)
GCD(12, 9) = GCD(9 , 12 mod 9) = GCD(9, 3)
GCD(9,3) = GCD(3 , 9 mod 3) = GCD(3, 0) = 3
GCD(33,12) = 2
Understanding GCD Example 2
Find the GCD(5280, 4680)
Q A B R
1 5280 4680 600
4 480 120 0
120 0
GCD(5280,4680) = 120
EXTENDED EUCLIDEAN ALGORITHM
• If a and b are any positive integers not both zero, GCD(a , b) is the
smallest positive element of the set
• Thus :
sa + tb = d = GCD(a , b)
s and t are Bézout's identity (also called Bézout's lemma).
initialize s1 = 1, s2 = 0 : t1 = 0 , t2 = 1
s = s1 – q(quotient)s2
t = t1 – q t2
• So extended Euclidean can calculate GCD(a, b) and the values of s and t
(Bézout's identity ).
Euclid’s Extended Algorithm for finding GCD :
Find the GCD(161,28)
q a b r s1 s2 s t1 t2 t
Euclid’s Extended Algorithm for finding GCD :
Find the GCD(161,28)
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
7 0 -1 4 6 -23
GCD(161 , 28) = 7
s = -1
t=6
sa + tb = d = GCD(a , b)
(-1)*(161) + (6)*(28) = 7
d=7
• Euclid’s Extended Algorithm for finding GCD example 2 :
q a b r s1 s2 s t1 t2 t
17 0 1 0 0 1
GCD(17,0) = 17
1*17 + 0*0 = 17 = d