Karpagam Academy of Higher Education Design & Analysis of Algorithms, 1-14
Euclid’s AlgorithmEuclid’s Algorithm
Problem: Find gcd(Problem: Find gcd(m,nm,n), the greatest common divisor of two ), the greatest common divisor of two
nonnegative, not both zero integers nonnegative, not both zero integers m m and and nn
Examples: gcd(60,24) = 12, gcd(60,0) = 60, gcd(0,0) = ? Examples: gcd(60,24) = 12, gcd(60,0) = 60, gcd(0,0) = ?
Euclid’s algorithm is based on repeated application of equalityEuclid’s algorithm is based on repeated application of equality
gcd(gcd(m,nm,n) = gcd() = gcd(n, m n, m mod mod nn))
until the second number becomes 0, which makes the problemuntil the second number becomes 0, which makes the problem
trivial.trivial.
Example: gcd(60,24) = gcd(24,12) = gcd(12,0) = 12Example: gcd(60,24) = gcd(24,12) = gcd(12,0) = 12