Greatest common divisors and Euclid’s algorithm [01DZ]
- October 13, 2026
- Marcelo Fiore, Jon Sterling
Picking up from last time, we return to our idea for a recursive calculation of greatest common divisors.
Idea Recursive calculation of greatest common divisors [01DX]
- October 13, 2026
- Marcelo Fiore, Jon Sterling
Although it is not easy to calculate the set of all common divisors of two positive integers m,n, it will turn out to be easy to calculate the greatest common divisor \gcd {\mathopen {}\left (m,n\right )\mathclose {}}, and our characterisation of common divisors gives us an idea why:
- If n\mid m, then we know that \mathsf {CD}{\mathopen {}\left (m,n\right )\mathclose {}} = \mathsf {D}{\mathopen {}\left (n\right )\mathclose {}} and hence \gcd {\mathopen {}\left (m,n\right )\mathclose {}} = n.
- Otherwise, we know that \mathsf {CD}{\mathopen {}\left (m,n\right )\mathclose {}} = \mathsf {CD}{\mathopen {}\left (n,\operatorname {rem}{\mathopen {}\left (m,n\right )\mathclose {}}\right )\mathclose {}}, so \gcd {\mathopen {}\left (m,n\right )\mathclose {}} = \gcd {\mathopen {}\left (n,\operatorname {rem}{\mathopen {}\left (m,n\right )\mathclose {}}\right )\mathclose {}}.
In summary, we have:
\gcd {\mathopen {}\left (m,n\right )\mathclose {}} = \begin {cases} n &\text {if } n\mid m \\ \gcd {\mathopen {}\left (n,\operatorname {rem}{\mathopen {}\left (m,n\right )\mathclose {}}\right )\mathclose {}} & \text {otherwise} \end {cases}The above is circular and so it isn’t automatically a valid definition; but it is suggestive of an algorithm…
The algorithm suggested by the above is called Euclid’s Algorithm, which we write out in OCaml code below.
Algorithm Euclid’s algorithm for calculating the gcd [01E0]
- October 13, 2026
- Marcelo Fiore, Jon Sterling
let rec gcd (m, n) = let r = rem (m, n) in if r = 0 then n else gcd (n, r)
Above, we have used the division algorithm.
Example A trace of Euclid’s algorithm [01E2]
- October 13, 2026
- Marcelo Fiore, Jon Sterling
| \gcd {\mathopen {}\left (m,n\right )\mathclose {}} |
|---|
| \gcd {\mathopen {}\left (34,13\right )\mathclose {}}{} |
| \gcd {\mathopen {}\left (13,8\right )\mathclose {}}{} |
| \gcd {\mathopen {}\left (8,5\right )\mathclose {}}{} |
| \gcd {\mathopen {}\left (5,3\right )\mathclose {}}{} |
| \gcd {\mathopen {}\left (3,2\right )\mathclose {}}{} |
| \gcd {\mathopen {}\left (2,1\right )\mathclose {}}{} |
| 1{} |
Warning Going from ideas to algorithms is not automatic… [01E1]
- October 13, 2026
- Jon Sterling
Instead of Idea [01DX], we might have just as well considered the following recursive characterisation of the greatest common divisor, where we put the remainder in the first parameter instead of the second:
\gcd {\mathopen {}\left (m,n\right )\mathclose {}} = \begin {cases} n & \text {if } n\mid m\\ \gcd {\mathopen {}\left (\operatorname {rem}{\mathopen {}\left (m,n\right )\mathclose {}},n\right )\mathclose {}} &\text {otherwise} \end {cases}This is correct because the greatest common divisor of two numbers does not depend on the order in which they are given. From here, we might have then been led to consider the following “algorithm” for computing the gcd:
let rec gcd' (m, n) = let (q, r) = divide (m, n) in if r = 0 then n else gcd' (r, n)
The above is valid OCaml code, but it will not compute the gcd. How is this possible? The issue is that it is does not terminate on all inputs, which we can easily illustrate by comparison with our prior example trace of Euclid’s algorithm:
\begin {aligned} \mathrm {gcd}'{\mathopen {}\left (13,34\right )\mathclose {}} &\leadsto \mathrm {gcd}'{\mathopen {}\left (13,34\right )\mathclose {}} \\ &\leadsto \mathrm {gcd}'{\mathopen {}\left (13,34\right )\mathclose {}} \\ &\leadsto \mathrm {gcd}'{\mathopen {}\left (13,34\right )\mathclose {}} \\ &\cdots \end {aligned}The problem is that the remainder \operatorname {rem}{\mathopen {}\left (13,34\right )\mathclose {}} is 13, so the parameter never gets smaller. The real Euclid’s algorithm essentially swaps the denominator in every recursive call, which causes the parameters to eventually decrease until termination. We will see a little later how to rigorously argue that the algorithm actually terminates.
We will shortly now show the correctness of Euclid’s algorithm; to that end, we first recall the definition of greatest common divisors:
Definition Greatest common divisor [01E3]
- October 13, 2026
- Marcelo Fiore, Jon Sterling
The greatest common divisor of two natural numbers m,n:\mathbb {N} is defined to be the unique common divisor k\in \mathsf {CD}{\mathopen {}\left (m,n\right )\mathclose {}} satisfying following universal property: every common divisor of m and n divides k.
We will write \gcd {\mathopen {}\left (m,n\right )\mathclose {}} for the greatest common divisor of m and n. Summarising:
\gcd {\mathopen {}\left (m,n\right )\mathclose {}}\in \mathsf {CD}{\mathopen {}\left (m,n\right )\mathclose {}} \qquad \forall d: \mathsf {CD}{\mathopen {}\left (m,n\right )\mathclose {}}\mathpunct {.} d\mid \gcd {\mathopen {}\left (m,n\right )\mathclose {}}Observation Greatest common divisor in terms of subset extensionality [01E7]
- October 19, 2026
- Marcelo Fiore, Jon Sterling
For all natural numbers m, n, and k, the following statements are equivalent:
- \mathsf {CD}{\mathopen {}\left (m,n\right )\mathclose {}} = \mathsf {D}{\mathopen {}\left (k\right )\mathclose {}};
- k is the greatest common divisor of m and n.
After attempting to prove this yourself, you may click this text to see my proof.
Proof.
- October 19, 2026
- Marcelo Fiore, Jon Sterling
This follows immediately via the principle of subset extensionality by unfolding the definitions of common divisors (\mathsf {CD}) and divisors (\mathsf {D}). Try it!
Theorem Correctness of Euclid’s algorithm [01E5]
- October 13, 2026
- Marcelo Fiore, Jon Sterling
Euclid’s algorithm gcd, reproduced below, terminates on all pairs of positive integers and, for such m and n, the value of gcd (m, n) is the greatest common divisor of m and n.
let rec gcd (m, n) = let r = rem (m, n) in if r = 0 then n else gcd (n, r)
Proof.
- October 13, 2026
- Marcelo Fiore, Jon Sterling
We consider how gcd (m, n) may evaluate, recalling an illustrative trace:
| \gcd {\mathopen {}\left (m,n\right )\mathclose {}} |
|---|
| \gcd {\mathopen {}\left (34,13\right )\mathclose {}}{} |
| \gcd {\mathopen {}\left (13,8\right )\mathclose {}}{} |
| \gcd {\mathopen {}\left (8,5\right )\mathclose {}}{} |
| \gcd {\mathopen {}\left (5,3\right )\mathclose {}}{} |
| \gcd {\mathopen {}\left (3,2\right )\mathclose {}}{} |
| \gcd {\mathopen {}\left (2,1\right )\mathclose {}}{} |
| 1{} |
We now argue for termination. We note that the second argument to gcd strictly decreases in every recursive call:
- If m<n, then
gcd (m, n)will reduce togcd (n, m): this is because thequo (m, n)reduces to 0 andrem (m, n)reduces to m. - If m \geq n, then either
gcd (m, n)returns n or it reduces to something equivalent togcd (n, rem (m, n)). The value ofrem (m, n)is strictly less than n.
Because the entire algorithm takes is defined using operations that are defined on the natural numbers, the arguments never become negative. For this reason, we conclude that the computation must eventually terminate.
Now we wish to argue that when gcd (m, n) does terminate, it does so with the actual greatest common divisor of m and n. By our termination argument, we have a finite trace of recursive calls leading to a return value:
Let q and r be the quotient and remainder respectively of m and n, so that r<n and m = q\cdot n + r. We will proceed by induction on the trace (I know that we haven’t taught you induction yet; bear with me).
- If the trace returns right away, then that was because the remainder of m and n was 0, so n\mid m and therefore n is the greatest common divisor of m and n, and the return value was n, so we are done.
- Otherwise, we may assume by induction that
gcd (i_0, j_0)does return the greatest common divisor of i_0 and j_0, and we need to show that this is also the greatest common divisor of m and n. In this case, we know that the remainder of m and n was some positive integer r, and that i_0 = n and j_0=r. But now we recall our characterisation of common divisor sets, namely that the common divisors of m and n are the same as the common divisors of n and the remainder of m and n, so we have the following equation: \mathsf {CD}{\mathopen {}\left (m,n\right )\mathclose {}} = \mathsf {CD}{\mathopen {}\left (n,\operatorname {rem}{\mathopen {}\left (m,n\right )\mathclose {}}\right )\mathclose {}} = \mathsf {D}{\mathopen {}\left (\gcd {\mathopen {}\left (n,\operatorname {rem}{\mathopen {}\left (m,n\right )\mathclose {}}\right )\mathclose {}}\right )\mathclose {}} Then by Observation [01E7], we conclude that the greatest common divisor of m and n is the greatest common divisor of n and \operatorname {rem}{\mathopen {}\left (m,n\right )\mathclose {}}, so we are done.