Lecture 8: Euclid’s Algorithm, properties of gcds, Euclid’s Theorem, extended Euclid’s algorithm [01DY]
- October 13, 2026
- Marcelo Fiore, Jon Sterling
These lecture notes were prepared by Jon Sterling using Marcelo Fiore’s lectures as source material. Any mistakes were introduced by Jon Sterling. Generative AI was not used to write these lecture notes.
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.
Example Fractions in lowest terms [01EC]
- October 20, 2026
- Marcelo Fiore, Jon Sterling
In a previous lecture, we gave a non-constructive proof that rational numbers are characterised by fractions m/n such that no prime divides both m and n.
What we meant by “non-constructive” is that the proof did not provide any clarity on which m and n correspond to a given rational x. Euclid’s algorithm gives us a way to compute the actual fraction in question:
let lowest_terms (m, n) = let k = gcd (m, n) in (quo (m, k), quo (n, k))
Properties of greatest common divisors [01E9]
- October 20, 2026
- Marcelo Fiore, Jon Sterling
Corollary [01ED]
- October 20, 2026
- October 20, 2026
- Marcelo Fiore, Jon Sterling
Let m and n be integers.
- For all integers k and l and g\in \mathsf {CD}{\mathopen {}\left (m,n\right )\mathclose {}} we have g\mid {\mathopen {}\left (k\cdot m + l\cdot n\right )\mathclose {}} . In other words, a common divisor of m and n divides any integer linear combination of m and n.
- If 1 is an integer linear combination of m and n, then m and n are coprime in the sense that \gcd {\mathopen {}\left (m,n\right )\mathclose {}} = 1.
After attempting to prove this yourself, you may click this text to see my proof.
Proof of (1).
- October 20, 2026
- October 20, 2026
- Marcelo Fiore, Jon Sterling
Fix k and l and g such that g\mid m and g\mid n. Then m = g\cdot \operatorname {quo}{\mathopen {}\left (m,g\right )\mathclose {}} and n = g\cdot \operatorname {quo}{\mathopen {}\left (n,g\right )\mathclose {}} and hence k\cdot m + l\cdot n = k\cdot g \cdot \operatorname {quo}{\mathopen {}\left (m,g\right )\mathclose {}} + l \cdot g \cdot \operatorname {quo}{\mathopen {}\left (n,g\right )\mathclose {}} = g\cdot {\mathopen {}\left (k\cdot \operatorname {quo}{\mathopen {}\left (m,g\right )\mathclose {}} + l\cdot \operatorname {quo}{\mathopen {}\left (n,g\right )\mathclose {}}\right )\mathclose {}} and so g\mid k\cdot m + l\cdot n.
Proof of (2).
- October 20, 2026
- October 20, 2026
- Marcelo Fiore, Jon Sterling
We recall the definition of integer linear combination: we assume some k,l:\mathbb {Z} such that k\cdot m + l\cdot n = 1 and must prove that \gcd {\mathopen {}\left (m,n\right )\mathclose {}} = 1. By (1), we have \gcd {\mathopen {}\left (m,n\right )\mathclose {}}\mid 1 which immediately implies \gcd {\mathopen {}\left (m,n\right )\mathclose {}}=1.
We will now establish some important algebraic laws concerning greatest common divisors. In fact, if you knew just a little bit about preorders we would be able to derive what follows in vastly greater generality with no mucking about with numbers whatsoever; ask me about category theory if you want to know why!
Lemma \gcd is commutative [01EE]
- October 20, 2026
- Marcelo Fiore, Jon Sterling
For all positive integers m and n, we have \gcd {\mathopen {}\left (m,n\right )\mathclose {}}=\gcd {\mathopen {}\left (n,m\right )\mathclose {}}\text {.}
After attempting to prove this yourself, you may click this text to see my proof.
Proof.
- October 20, 2026
- Marcelo Fiore, Jon Sterling
We recall from Observation [01E7] that \gcd {\mathopen {}\left (m,n\right )\mathclose {}} = k if and only if \mathsf {CD}{\mathopen {}\left (m,n\right )\mathclose {}} = \mathsf {D}{\mathopen {}\left (k\right )\mathclose {}}. Therefore, it suffices to show observe \mathsf {CD}{\mathopen {}\left (m,n\right )\mathclose {}} = \mathsf {CD}{\mathopen {}\left (n,m\right )\mathclose {}} = \mathsf {D}{\mathopen {}\left (\gcd {\mathopen {}\left (n,m\right )\mathclose {}}\right )\mathclose {}} \text {.}
Lemma \gcd is associative [01EF]
- October 20, 2026
- Marcelo Fiore, Jon Sterling
For all positive integers l, m, and n, we have \gcd {\mathopen {}\left (l,\gcd {\mathopen {}\left (m,n\right )\mathclose {}}\right )\mathclose {}}=\gcd {\mathopen {}\left (\gcd {\mathopen {}\left (l,m\right )\mathclose {}},n\right )\mathclose {}}
After attempting to prove this yourself, you may click this text to see my proof.
Proof using Observation [01E7].
- October 20, 2026
- Marcelo Fiore, Jon Sterling
By Observation [01E7], it suffices to show \mathsf {CD}{\mathopen {}\left (l,\gcd {\mathopen {}\left (m,n\right )\mathclose {}}\right )\mathclose {}} = \mathsf {D}{\mathopen {}\left (\gcd {\mathopen {}\left (\gcd {\mathopen {}\left (l,m\right )\mathclose {}},n\right )\mathclose {}}\right )\mathclose {}} \text {.} We calculate using the principle of subset extensionality as follows, using the fact that subset intersection is associative:
\begin {aligned} \mathsf {CD}{\mathopen {}\left (l,\gcd {\mathopen {}\left (m,n\right )\mathclose {}}\right )\mathclose {}} &= \mathsf {D}{\mathopen {}\left (l\right )\mathclose {}}\cap \mathsf {D}{\mathopen {}\left (\gcd {\mathopen {}\left (m,n\right )\mathclose {}}\right )\mathclose {}} \\ &= \mathsf {D}{\mathopen {}\left (l\right )\mathclose {}}\cap \mathsf {CD}{\mathopen {}\left (m,n\right )\mathclose {}} \\ &= \mathsf {D}{\mathopen {}\left (l\right )\mathclose {}}\cap {\mathopen {}\left ( \mathsf {D}{\mathopen {}\left (m\right )\mathclose {}}\cap \mathsf {D}{\mathopen {}\left (n\right )\mathclose {}} \right )\mathclose {}} \\ &= {\mathopen {}\left (\mathsf {D}{\mathopen {}\left (l\right )\mathclose {}}\cap \mathsf {D}{\mathopen {}\left (m\right )\mathclose {}}\right )\mathclose {}} \cap \mathsf {D}{\mathopen {}\left (n\right )\mathclose {}} \\ &= \mathsf {CD}{\mathopen {}\left (l,m\right )\mathclose {}}\cap \mathsf {D}{\mathopen {}\left (n\right )\mathclose {}} \\ &= \mathsf {D}{\mathopen {}\left (\gcd {\mathopen {}\left (l,m\right )\mathclose {}}\right )\mathclose {}}\cap \mathsf {D}{\mathopen {}\left (n\right )\mathclose {}} \\ &= \mathsf {CD}{\mathopen {}\left (\gcd {\mathopen {}\left (l,m\right )\mathclose {}},\mathsf {D}{\mathopen {}\left (n\right )\mathclose {}}\right )\mathclose {}} \\ &= \mathsf {D}{\mathopen {}\left ( \gcd {\mathopen {}\left ( \gcd {\mathopen {}\left (l,m\right )\mathclose {}}, n \right )\mathclose {}} \right )\mathclose {}} \end {aligned}Proof by universal property.
- October 20, 2026
- Marcelo Fiore, Jon Sterling
Alternatively, we can give a direct proof using the universal property of the greatest common divisor. It suffices to show that for all naturals d, we have d\mid \gcd {\mathopen {}\left (l,\gcd {\mathopen {}\left (m,n\right )\mathclose {}}\right )\mathclose {}} \Longleftrightarrow d\mid \gcd {\mathopen {}\left (\gcd {\mathopen {}\left (l,m\right )\mathclose {}},n\right )\mathclose {}}\text {.}
We calculate:
\begin {aligned} d \mid \gcd {\mathopen {}\left (l,\gcd {\mathopen {}\left (m,n\right )\mathclose {}}\right )\mathclose {}} &\Longleftrightarrow d \mid l \land d\mid \gcd {\mathopen {}\left (m,n\right )\mathclose {}} \\ &\Longleftrightarrow d\mid l \land {\mathopen {}\left (d \mid m\land d\mid n\right )\mathclose {}} \\ &\Longleftrightarrow {\mathopen {}\left (d\mid l\land d\mid m\right )\mathclose {}}\land d\mid n \\ &\Longleftrightarrow d\mid \gcd {\mathopen {}\left (l,m\right )\mathclose {}}\land d\mid n \\ &\Longleftrightarrow d\mid \gcd {\mathopen {}\left (\gcd {\mathopen {}\left (l,m\right )\mathclose {}},n\right )\mathclose {}} \end {aligned}Both proofs are correct! In some cases one style might be easier than the other. Universal properties are very powerful and one should not be afraid to use them directly.
Lemma \gcd is linear [01EG]
- October 20, 2026
- Marcelo Fiore, Jon Sterling
For positive integers l, m, and n we have l\cdot \gcd {\mathopen {}\left (m,n\right )\mathclose {}} = \gcd {\mathopen {}\left (l\cdot m, l\cdot n\right )\mathclose {}}\text {.}
Hint: for this one, try thinking about how Euclid’s algorithm computes.
After attempting to prove this yourself, you may click this text to see my proof.
Proof.
- October 20, 2026
- Marcelo Fiore, Jon Sterling
We recall Euclid’s algorithm below:
let rec gcd (m, n) = let r = rem (m, n) in if r = 0 then n else gcd (n, r)
We consider the case where both arguments are multiplied by l:
gcd (l * m, l * n) = let r = rem (l * m, l * n) in if r = 0 then l * n else gcd (l * n, r)
Note that rem (l * m, l * n) = l * (rem (m, n)).
gcd (l * m, l * n) = let r = l * (rem (m, n)) in if r = 0 then l * n else gcd (l * n, r)
Furthemore, because l is assumed positive, l * (rem (m, n)) is equal to zero if and only if rem (m, n) is. So we may rewrite as follows:
gcd (l * m, l * n) = let r = rem (m, n) in if r = 0 then l * n else gcd (l * n, l * r)
We now consider the entire trace of recursive calls. If gcd (l * m, l * n) returns straightaway, we must have had rem (m, n) = 0 and it returns l * n which is indeed l * (gcd (m, n)). If there is a recursive call to gcd (l * n, l * r) we may assume by induction that gcd (l * n, l * r) = l * (gcd (n, r)), which is exactly what we needed.
Coprimality and Euclid’s theorem [01EH]
- October 20, 2026
- Marcelo Fiore, Jon Sterling
Definition Coprime integers [01EI]
- October 20, 2026
- Marcelo Fiore, Jon Sterling
Two natural numbers are said to be coprime when their greatest common divisor is 1.
Lemma [01EJ]
- October 20, 2026
- Marcelo Fiore, Jon Sterling
Let k and m be coprime and let n be an arbitrary integer. If k\mid {\mathopen {}\left (m\cdot n\right )\mathclose {}}, then k\mid n.
After attempting to prove this yourself, you may click this text to see my proof.
Proof.
- October 20, 2026
- Marcelo Fiore, Jon Sterling
By our assumption k\mid {\mathopen {}\left (m\cdot n\right )\mathclose {}} we may assume some integer l such that k\cdot l = m\cdot n. We now reason using our assumptions, the definition of coprimality, and the linearity of \gcd :
\begin {aligned} n &= 1\cdot n \\ &= \gcd {\mathopen {}\left (k,m\right )\mathclose {}} \cdot n \\ &= \gcd {\mathopen {}\left (k\cdot n, m\cdot n\right )\mathclose {}} \\ &= \gcd {\mathopen {}\left (k\cdot n, k\cdot l\right )\mathclose {}} \\ &= k\cdot \gcd {\mathopen {}\left (n,l\right )\mathclose {}} \end {aligned}Therefore, k\mid n.
Ironically, Euclid’s Theorem is going to be a corollary of the above.
Corollary Euclid’s Theorem [01EK]
- October 20, 2026
- Marcelo Fiore, Jon Sterling
For all integers m and n and a prime p, if p\mid {\mathopen {}\left (m\cdot n\right )\mathclose {}} then either p\mid m or p\mid n.
After attempting to prove this yourself, you may click this text to see my proof.
Proof.
- October 20, 2026
- Marcelo Fiore, Jon Sterling
If either m and n is zero, then the claim is trivial. We now assume without loss of generality that m and n are positive.
To show p\mid m \lor p \mid n it suffices to show that p\nmid m \implies p \mid n and p\nmid n\implies p\mid m. As these cases are symmetric, we consider just the first one.
Assume p \nmid m. The only candidates for common divisors of p and m are p and 1 because p is prime, and we have already ruled out p by our assumption p\nmid m. Hence \gcd {\mathopen {}\left (p,m\right )\mathclose {}} = 1 and thus p and m are coprime.
By Lemma [01EJ], then, p\mid {\mathopen {}\left (m\cdot n\right )\mathclose {}} implies p\mid n so we are done.
We now return to Fermat’s Little Theorem, which we did not properly have the tools to prove when we first encountered it.
Theorem Fermat’s Little Theorem [01BK]
- September 29, 2026
- Marcelo Fiore, Jon Sterling
For all natural numbers i and primes p, we have the following:
- i^p \equiv i \mod p
- i^{p-1}\equiv 1 \mod p unless i is a multiple of p
After attempting to prove this yourself, you may click this text to see my proof.
Proof. Proof of Fermat’s Little Theorem [01EL]
- October 20, 2026
- Marcelo Fiore, Jon Sterling
The first property is precisely the many dropout lemma, which states (0+i)^p \equiv 0^p+i \equiv i \mod p.
For the second part, we assume that p\nmid i and must show that i^{p-1}\equiv 1 \mod p. We consider the following sequence of equivalences, where the first statement holds by the first part of the theorem:
\begin {aligned} i^p\equiv i \mod p &\Longleftrightarrow p\mid {\mathopen {}\left (i^p - i\right )\mathclose {}} \\ &\Longleftrightarrow p\mid i\cdot {\mathopen {}\left (i^{p-1}-1\right )\mathclose {}} \end {aligned}By Euclid’s Thoerem, it therefore follows that either p\mid i or p\mid {\mathopen {}\left (i^{p-1}-1\right )\mathclose {}}. The former is ruled out by assumption, and the latter is equivalent by definition to i^{p-1}\equiv 1 \mod p.
Corollary [01EM]
- October 20, 2026
- Marcelo Fiore, Jon Sterling
For all primes p and integers m such that 0<m<p, we have p\mid {p \choose m} and {\mathopen {}\left (p-m\right )\mathclose {}}\mid {{p-1}\choose m}.
After attempting to prove this yourself, you may click this text to see my proof.
Proof.
- October 20, 2026
- Marcelo Fiore, Jon Sterling
Fix a prime p and an integer m such that 0<m<p.
We first notice p\cdot {{p-1}\choose m} = {p \choose m}\cdot (p-m): thinking combinatorically, the former describes the process of choosing one member from a group of p and then choosing m members from what's left; we equally well could first choose m members from p, and then one member of the remaining p-m.
Because 0<m and p is prime, the greatest common divisor of p and p-m is 1, so p and p-m are coprime.
- To show that p\mid \binom {p}{m}, by Lemma [01EJ] it suffices to show that p\mid {\mathopen {}\left (p-m\right )\mathclose {}}\cdot \binom {p}{m} because p and p-m are coprime. By our combinatoric reasoning, this is the same as p\mid p\cdot \binom {p-1}{m} which immediately holds.
- To show that {\mathopen {}\left (p-m\right )\mathclose {}}\mid \binom {p-1}{m} it suffices by Lemma [01EJ] to show that {\mathopen {}\left (p-m\right )\mathclose {}}\mid p\cdot \binom {p-1}{m} which is equivalent to {\mathopen {}\left (p-m\right )\mathclose {}}\mid \binom {p}{m}\cdot {\mathopen {}\left (p-m\right )\mathclose {}} which immediately holds.
Corollary Integers modulo a prime form a field [01EN]
- October 20, 2026
- Marcelo Fiore, Jon Sterling
For a prime p, every non-zero element i of the modular integers \mathbb {Z}_p has {\mathopen {}\left [i^{p-2}\right ]\mathclose {}}_p as multiplicative inverse. Hence the ring \mathbb {Z}_p is a field.
After attempting to prove this yourself, you may click this text to see my proof.
Proof.
- October 20, 2026
- Marcelo Fiore, Jon Sterling
We want to check the following identity:
i \cdot _p {\mathopen {}\left [i^{p-2}\right ]\mathclose {}}_p = 1Equivalently, we must show i\cdot i^{p-2} \equiv 1, or equivalently i^{p-1}\equiv 1\mod p. This is the second part of Fermat’s little theorem, noting that i<p so i\nmid p.
Extended Euclid’s Algorithm; integer linear combinations II [01EO]
- October 20, 2026
- Marcelo Fiore, Jon Sterling
There is more to Euclid’s algorithm than meets the eye. Let us first return to an illustrative trace of the 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{} |
Let’s annotate each step of Example [01E2] with the quotient and remainder that was computed:
| \gcd {\mathopen {}\left (m,n\right )\mathclose {}} | m = q\cdot n + r |
|---|---|
| \gcd {\mathopen {}\left (34,13\right )\mathclose {}}{} | 34 = 2\cdot 13 + 8{} |
| \gcd {\mathopen {}\left (13,8\right )\mathclose {}}{} | 13 = 1\cdot 8 + 5{} |
| \gcd {\mathopen {}\left (8,5\right )\mathclose {}}{} | 8 = 1\cdot 5 + 3{} |
| \gcd {\mathopen {}\left (5,3\right )\mathclose {}}{} | 5 = 1\cdot 3 + 2{} |
| \gcd {\mathopen {}\left (3,2\right )\mathclose {}}{} | 3 = 1\cdot 2 + 1{} |
| \gcd {\mathopen {}\left (2,1\right )\mathclose {}}{} | 2 = 2\cdot 1 + 0{} |
| 1{} | {} |
What is interesting is that in each step, we can express the quotient and the remainder as an integer linear combination of the two arguments to \gcd :
| \gcd {\mathopen {}\left (m,n\right )\mathclose {}} | m = q\cdot n + r | r = m + j \cdot n |
|---|---|---|
| \gcd {\mathopen {}\left (34,13\right )\mathclose {}}{} | 34 = 2\cdot 13 + 8{} | 8 = 34 - 2\cdot 13{} |
| \gcd {\mathopen {}\left (13,8\right )\mathclose {}}{} | 13 = 1\cdot 8 + 5{} | 5 = 13 - 1\cdot 8{} |
| \gcd {\mathopen {}\left (8,5\right )\mathclose {}}{} | 8 = 1\cdot 5 + 3{} | 3 = 8 - 1\cdot 5{} |
| \gcd {\mathopen {}\left (5,3\right )\mathclose {}}{} | 5 = 1\cdot 3 + 2{} | 2 = 5 - 1\cdot 3{} |
| \gcd {\mathopen {}\left (3,2\right )\mathclose {}}{} | 3 = 1\cdot 2 + 1{} | 1 = 3 - 1\cdot 2{} |
| \gcd {\mathopen {}\left (2,1\right )\mathclose {}}{} | 2 = 2\cdot 1 + 0{} | {} |
| 1{} | {} | {} |
Remark Infinitude of linear combinations [01EQ]
- October 28, 2026
- Marcelo Fiore, Jon Sterling
Note that there are infinitely many ways in which an integer can be expressed as a linear combination is infinite. Indeed, for integers m, n, r, s, t, we have \begin {bmatrix} s & t \end {bmatrix} \otimes \begin {bmatrix} m \\ n \end {bmatrix} = r if and only of for all integers k, \begin {bmatrix} (s+k\cdot n) & (t - k\cdot m) \end {bmatrix} \otimes \begin {bmatrix} m \\ n \end {bmatrix} = r\text {.}
Proof.
- October 28, 2026
- Marcelo Fiore, Jon Sterling
Indeed, we have:
\begin {aligned} \begin {bmatrix} (s+k n) & (t - k m) \end {bmatrix} \otimes \begin {bmatrix} m \\ n \end {bmatrix} &= (s+k n)\cdot m + (t - k m)\cdot n \\ &= sm + k n m + tn - kmn \\ &= sm + tn \end {aligned}The proof of the following theorem is left as an exercise to the reader: it can be done using the extended Euclid’s algorithm, which we will define soon.
Theorem Greatest common divisors as linear combinations [01ER]
- October 28, 2026
- Marcelo Fiore, Jon Sterling
For all positive integers m and n,
- \gcd {\mathopen {}\left (m,n\right )\mathclose {}} is an integer linear combination of m and n;
- we may efficiently compute a pair \operatorname {lc}_1{\mathopen {}\left (m,n\right )\mathclose {}},\operatorname {lc}_2{\mathopen {}\left (m,n\right )\mathclose {}} of integer coefficients for it, so that \begin {bmatrix} \operatorname {lc}_1{\mathopen {}\left (m,n\right )\mathclose {}}& \operatorname {lc}_2{\mathopen {}\left (m,n\right )\mathclose {}} \end {bmatrix} \otimes \begin {bmatrix} m \\ n \end {bmatrix} = \gcd {\mathopen {}\left (m,n\right )\mathclose {}}
The proof of the above relies on the following elementary properties of linear combinations:
Proposition Basic properties of integer linear combinations [01ES]
- October 28, 2026
- Marcelo Fiore, Jon Sterling
For all integers m and n:
- We have \begin {bmatrix}1 & 0 \end {bmatrix}\otimes \begin {bmatrix}m\\ n\end {bmatrix} = m and \begin {bmatrix}0 & 1 \end {bmatrix}\otimes \begin {bmatrix}m\\ n\end {bmatrix} = n\text {.}
- For all integers s_1,t_1,r_1 and s_2,t_2,r_2, \begin {bmatrix} s_1 & t_1 \end {bmatrix} \otimes \begin {bmatrix} m\\ n \end {bmatrix} = r_1 \land \begin {bmatrix} s_2 & t_2 \end {bmatrix} \otimes \begin {bmatrix} m\\ n \end {bmatrix} = r_2 implies \begin {bmatrix} s_1+s_2 & t_1+t_2 \end {bmatrix} \otimes \begin {bmatrix} m\\ n \end {bmatrix} = r_1+r_2\text {.}
- For all integers k and s,t,r, \begin {bmatrix}s & t\end {bmatrix}\otimes \begin {bmatrix}m \\ n\end {bmatrix} = r implies \begin {bmatrix}k\cdot s & k\cdot t\end {bmatrix} \otimes \begin {bmatrix}m\\ n\end {bmatrix} = k\cdot r \text {.}
Extending Euclid’s algorithm [01ET]
- October 28, 2026
- Marcelo Fiore, Jon Sterling
We shall now extend Euclid’s algorithm \gcd {\mathopen {}\left (m,n\right )\mathclose {}} from computing on pairs of positive integers to computing on pairs of triples of integers ((s,t),r) with r positive, satisfying the invariant that s and t are coefficients representing r as a linear combination of m and n.
Algorithm Extended Euclid’s Algorithm [01EU]
- October 28, 2026
- Marcelo Fiore, Jon Sterling
We define a function
egcd : int * int -> (int * int) * int
such that egcd (m,n) computes a triple ((s,t), r) such that r is the greatest common divisor of m and n, and s and t are linear coefficients representing r as an integer linear combination of m and n.
In other words, the specification of egcd is that the value of egcd (m,n) is the triple {\mathopen {}\left ({\mathopen {}\left (\operatorname {lc}_1{\mathopen {}\left (m,n\right )\mathclose {}},\operatorname {lc}_2{\mathopen {}\left (m,n\right )\mathclose {}}\right )\mathclose {}}, \gcd {\mathopen {}\left (m,n\right )\mathclose {}}\right )\mathclose {}}. Here is the algorithm:
let rec iterate_egcd ((s1,t1),r1) ((s2,t2),r2) = let (q, r) = divide r1 r2 in if r = 0 then ((s2,t2),r2) else iterate_egcd ((s2,t2),r2) ((s1 - q * s2, t1 - q*t2), r) let egcd (m, n) = iterate_egcd ((1,0),m) ((0,1),n)
We used the division algorithm above. We may derive the ordinary gcd from the above, as well as the calculations of the coefficients representing the gcd as a linear combination of m and n:
let gcd_from_egcd (m, n) = snd (egcd (m, n)) let lc1 (m, n) = fst (fst (egcd (m, n))) let lc2 (m, n) = snd (fst (egcd (m, n)))
Example A trace of the extended Euclid’s algorithm [01EV]
- October 28, 2026
- Marcelo Fiore, Jon Sterling
egcd (13,34) |
iterate_egcd ((1,0),13) ((0,1),34)
|
iterate_egcd ((0,1),34) ((1,0),13)
|
iterate_egcd ((1,0),13) ((-2,1),8)
|
iterate_egcd ((-2,1),8) ((3,-1), 5)
|
iterate_egcd ((3,-1),5) ((-5,2),3)
|
iterate_egcd ((-5,2),3) ((8,-3),2)
|
iterate_egcd ((8,-3),2) ((-13,5),1)
|
((-13,5),1)
|
Proposition Flipping the linear coefficients of the greatest common divisors [01EX]
- October 28, 2026
- Marcelo Fiore, Jon Sterling
For all distinct positive integers m and n, \operatorname {lc}_1{\mathopen {}\left (m,n\right )\mathclose {}} = \operatorname {lc}_2{\mathopen {}\left (n,m\right )\mathclose {}}.
Theorem Another universal property for the greatest common divisor [01EW]
- October 28, 2026
- Marcelo Fiore, Jon Sterling
For all positive integers m and n, \gcd {\mathopen {}\left (m,n\right )\mathclose {}} is the least positive linear combination of m and n.
Interpretation.
- October 28, 2026
- Marcelo Fiore, Jon Sterling
What this is saying is:
- There exist integer coefficients s,t such that s\cdot m + t \cdot n = \gcd {\mathopen {}\left (m,n\right )\mathclose {}}.
- For any s',t' and positive r = s'\cdot m + t'\cdot n, we have \gcd {\mathopen {}\left (m,n\right )\mathclose {}}\leq r.
After attempting to prove this yourself, you may click this text to see my proof.
Proof.
- October 28, 2026
- Marcelo Fiore, Jon Sterling
By Theorem [01ER], \gcd {\mathopen {}\left (m,n\right )\mathclose {}} is an integer linear combination of m and n.
That \gcd {\mathopen {}\left (m,n\right )\mathclose {}} is the least positive linear combination of m and n follows from the first part of Corollary [01ED], which states that for all integers k and l we have:
\gcd {\mathopen {}\left (m,n\right )\mathclose {}}\mid {\mathopen {}\left (k\cdot m + l\cdot n\right )\mathclose {}}In other words, \gcd {\mathopen {}\left (m,n\right )\mathclose {}} divides any integer linear combination of m and n. Therefore, unless k=l=0 we have \gcd {\mathopen {}\left (m,n\right )\mathclose {}}\leq k\cdot m + l\cdot n.