You are on page 1of 19

EUCLIDEAN AND

EXTENDED EUCLIDEAN
ALGORITHM

HOW TO FIND A GREATEST


COMMON DIVISOR IN SEVERAL
EASY STEPS
EUCLIDEAN ALGORITHM

• The well known Euclidean algorithm finds the


greatest common divisor of two numbers using
only elementary mathematical operations -
division and subtraction.
Euclidean Algorithm

Euclidean Algorithm or Euclid’s Algorithm.


For computing the Greatest Common Divisor(GCD).
And also know Highest Common Factor(HCF).
Euclidean Algorithm
• GCD(a,b) where a and b are integers.
• For all a,b with a>b there is a q(quotient) and
r(reminder) such that
a = qb + r
with r < b or r = 0
• This is calculated repeatedly by making a =b and b = r,
until r = 0.
• Finally ,GCD (b,0) = b.
FLOWCHART
PROPERTIES OF GCD

• GCD(b,0) = b
• GCD(a,a) = a
• GCD(a,b) = GCD(b,a mod b)

GCD can be find using 2 different methods


• Understanding GCD Example 1

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

Greatest Common Divisor(GCD) = 3


• Euclid’s Algorithm for finding GCD (methode 2):
Find the GCD(12,33)

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)

Factors of 5280 Factors of 4680


 1 2 3 4 5 6  1 2 3 4 5 6
   
   
 8 10 11 12 15 16  8 9 10 12 13 15
   
   
 20 22 24 30 32 33  18 20 24 26 30 36
   
   
 40 44 48 55 60 66  39 40 45 52 60 65
   
   
 80 88 96 110 120 132  72 78 90 104 117 120
   
   
160 165 176 220 240 264 130 156 180 195 234 260
   
   
330 352 440 480 528 660 312 360 390 468 520 585
   
   
880 1056 1320 1760 2640 5280 780 936 1170 1560 2340 4680
Euclid’s Algorithm for finding GCD (methode 1):
Find the GCD(5280,4680)

Q A B R
1 5280 4680 600

7 4680 600 480

1 600 480 120

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 :

Find the GCD(17,0)

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

This indicates why we should initialize s1=1, s2 =0 ; t1 = 0 , t2 = 1

You might also like