Euclidean Algorithm
Shruthi Srinivasan
2017PIS5174
The Euclidean Algorithm is a method used to compute GCD of two natural
numbers. It is named after the Greek Mathematician Euclid. GCD (Greatest
Common Divisior) of two numbers A and B is the largest integer that divides
both the numbers.
The function states that:
(
a(x) = 0 b(x) = 0
GCD(a(x), b(x)) =
GCD(b(x), a(x) mod b(x)) otherwise
Algorithm :
GCD(a(x),b(x))
1. If b(x) =0 then GCD = a(x), return GCD and stop
2. If b(x) != 0 then divide a(x) by b(x) and let r be the remainder. If r =0
then GCD =b(x) , return GCD and stop
3. Replace a(x) by b(x) and b(x) by r and return to step 2
Example :
To find GCD of 35 and 8
GCD(35, 8) = GCD(8, 3) (1)
= GCD(3, 2) (2)
= GCD(3, 2) (3)
= GCD(2, 1) (4)
= GCD(1, 0) (5)
=1 (6)
Source Code in Python:
1
d e f gcd ( a , b ) :
p r i n t C a l l i n g gcd(%d,%d ) %(a , b )
i f ( b==0) :
p r i n t \nGCD o f %d and %d i s %d %(num1 ,
num2 , a )
else :
gcd ( b , a%b )
print
p r i n t ( Program t o f i n d GCD o f two numbers u s i n g E u c l i d e a n
s a l g o r i t h m \n )
print
num1 = i n p u t ( Enter t h e f i r s t number )
num2 = i n p u t ( Enter t h e s e c o n d number )
gcd (num1 , num2)
Output: