In this chapter, we discuss Euclid's algorithm for computing greatest common divisors. It turns out that Euclid's algorithm has a number of very nice properties, and has applications far beyond that purpose.
The basic Euclidean algorithm
We consider the following problem: given two non-negative integers a and b, compute their greatest common divisor, gcd(a, b). We can do this using the well-known Euclidean algorithm, also called Euclid's algorithm.
The basic idea of Euclid's algorithm is the following. Without loss of generality, we may assume that a ≥ b ≥ 0. If b = 0, then there is nothing to do, since in this case, gcd(a, 0) = a. Otherwise, if b > 0, we can compute the integer quotient q ≔ └a/b┘ and remainder r ≔ a mod b, where 0 ≤ r < b. From the equation
it is easy to see that if an integer d divides both b and r, then it also divides a; likewise, if an integer d divides a and b, then it also divides r. From this observation, it follows that gcd(a, b) = gcd(b, r), and so by performing a division, we reduce the problem of computing gcd(a, b) to the “smaller” problem of computing gcd(b, r).