The division theorem and algorithm [01CR]
- September 30, 2026
- Marcelo Fiore, Jon Sterling
Our goal is to prove the division theorem for natural numbers, or at least explore some evidence for it (since we cannot rigorously prove it without induction, which we will learn later).
Theorem The Division Theorem [01CS]
- September 30, 2026
- Marcelo Fiore, Jon Sterling
For every natural number m and positive natural number n, there exists a unique pair of natural numbers (q,r) such that r < n and m = q\cdot n + r.
Definition Quotient and remainder [01CT]
- September 30, 2026
- Jon Sterling
Because the pair of naturals (q,r) associated to a given (m,n) by the division theorem are uniquely determined, we may express them as functions of (m,n). In particular, we shall write \operatorname {quo}{\mathopen {}\left (m,n\right )\mathclose {}} and \operatorname {rem}{\mathopen {}\left (m,n\right )\mathclose {}} for the unique naturals satisfying \operatorname {rem}{\mathopen {}\left (m,n\right )\mathclose {}}<n \quad \text {and}\quad m = \operatorname {quo}{\mathopen {}\left (m,n\right )\mathclose {}}\cdot n + \operatorname {rem}{\mathopen {}\left (m,n\right )\mathclose {}} \text {.}
Recalling our exploration of unique existence, a unique existence statement is often best proved by separately establishing uniqueness and existence. We will especially favour existence proofs that contain within them the idea of an algorithm, in this case the division algorithm.
First, we show uniqueness of the quotient and remainder.
Lemma Uniqueness of quotient and remainder [01CU]
- September 30, 2026
- Marcelo Fiore, Jon Sterling
Let m and n be naturals with n positive. Then any two pairs pairs of naturals (q_0,r_0) and (q_1,r_1) satisfying r_0,r_1<n and m=q_0n+r_0=q_1n+r_1 are equal.
Proof.
- September 30, 2026
- Marcelo Fiore, Jon Sterling
We will first show that r_0=r_1. We recall Example [01AZ] from Lecture 3, which states that congruence modulo n uniquely determines the natural numbers strictly below n. In particular, because we have r_0,r_1<n, to prove r_0=r_1 it suffices to show that r_0 and r_1 are congruent modulo n, i.e. r_0\equiv r_1\mod n.
To that end, we calculate the difference r_0-r_1:
\begin {aligned} r_0-r_1 &= (m - q_0n) - (m - q_1n) \\ &= m - q_0n - m + q_1n \\ &= q_0n + q_1n \\ &= n(q_0 + q_1) \end {aligned}Hence n\mid r_0-r_1 and thus r_0 \equiv r_1 \mod n from which it follows, by Example [01AZ], that r_0=r_1. Because they are equal, we will simply write r for either r_0 or r_1.
Now it remains to show that q_0=q_1. We have q_0n = r-m = q_1n; because n is assumed positive, we may use the multiplicative cancellation law for the naturals to deduce q_0 = q_1.
For existence, we first consider an algorithm to compute the quotient and remainder.
Algorithm The division algorithm [01CV]
- September 30, 2026
- Marcelo Fiore, Jon Sterling
We write the division algorithm in the notation of OCaml below:
let rec iterate_division n (q, r) : int * int = if r < n then (q, r) else iterate_division n (q + 1, r - n) let divide (m, n) : int * int = iterate_division n (0, m) let quo (m, n) = fst (divide (m, n)) let rem (m, n) = snd (divide (m, n))
Lemma Correctness of the division algorithm [01CW]
- September 30, 2026
- Marcelo Fiore, Jon Sterling
The division algorithm terminates on the appropriate inputs: for any natural number m and positive natural number n, the evaluation of \mathtt {divide}~m~n terminates with a pair {\mathopen {}\left (q,r\right )\mathclose {}} such that r < n and m = qn + r.
Proof.
- September 30, 2026
- Marcelo Fiore, Jon Sterling
Let m and n be natural numbers with n positive. We first consider the circumstances under which the evaluation of \mathtt {divide}~m~n terminates. We have \mathtt {divide}~m~n = \mathtt {iterate\_division}~n~(0,m) so \mathsf {terminates}(\mathtt {divide}~m~n) \Longleftrightarrow \mathsf {terminates}(\mathtt {iterate\_division}~n~(0,m)) \text {.}
\mathtt {iterate\_division}~n~(q,r) is defined so to terminate as soon as r gets small enough, and in each recursive call, r is getting smaller. Therefore, so long as n is positive, \mathtt {iterate\_division}~n~(q,r) is guaranteed to terminate and thus \mathtt {divide}~m~n is guaranteed to terminate.
We may now consider whether the values of \mathtt {divide}~m~n are pairs (q,r) such that r<n and m=qn+r. It is clear by inspection that the first condition holds. For the second, write P(q,r) for the statement m=qn+r. We will show that every call to \mathtt {iterate\_division} is with a pair satisfying P.
- The first call to \mathtt {iterate\_division} is with the pair (0,m), and we have m=0n+m.
- In the recursive call, we assume that P(q,r) holds and must show that P(q+1,r-n) holds. We see below that this is equivalent to our assumption: \begin {aligned} P(q+1,r-n) &\Longleftrightarrow m = (q+1)n + (r-n) \\ &\Longleftrightarrow m = qn+n + r - n \\ &\Longleftrightarrow m = qn + r \\ &\Longleftrightarrow P(q,r) \end {aligned}
We have shown above that if \mathtt {divide}~m~n terminates, then it terminates with a pair satisfying the desired property. Combining this with our earlier argument that \mathtt {divide}~m~n alwyas terminates, we are finished.
The division theorem therefore follows as a corollary of Lemma [01CU] and Lemma [01CW].
Proposition Congruence in terms of remainders [01CX]
- October 1, 2026
- Marcelo Fiore, Jon Sterling
Let m be a positive integer. For all natural numbers k and l, k \equiv l \mod m \Longleftrightarrow \operatorname {rem}{\mathopen {}\left (k,m\right )\mathclose {}} = \operatorname {rem}{\mathopen {}\left (l,m\right )\mathclose {}}
Proof.
- October 1, 2026
- Marcelo Fiore, Jon Sterling
Let m be positive, and fix arbitrary naturals k and l.
In the forward direction, we assume k \equiv l \mod m. Recalling that \operatorname {rem}{\mathopen {}\left (x,m\right )\mathclose {}} < m and the fact that equality of modular integers is congruence, it suffices to show \operatorname {rem}{\mathopen {}\left (k,m\right )\mathclose {}}\equiv \operatorname {rem}{\mathopen {}\left (l,m\right )\mathclose {}}\mod m\text {,} or in other words that m divides the difference between the two remainders. We introduce the following notations to simplify what follows:
\begin {aligned} \Delta {q} &\coloneqq \operatorname {quo}{\mathopen {}\left (k,m\right )\mathclose {}}-\operatorname {quo}{\mathopen {}\left (l,m\right )\mathclose {}} \\ \Delta {r} &\coloneqq \operatorname {rem}{\mathopen {}\left (k,m\right )\mathclose {}}-\operatorname {rem}{\mathopen {}\left (l,m\right )\mathclose {}} \end {aligned}We want to show that m\mid \Delta {r}. To isolate \Delta r we first consider the difference k-l:
\begin {aligned} k-l &= m\cdot \operatorname {quo}{\mathopen {}\left (k,m\right )\mathclose {}}+\operatorname {rem}{\mathopen {}\left (k,m\right )\mathclose {}} - {\mathopen {}\left (m\cdot \operatorname {quo}{\mathopen {}\left (l,m\right )\mathclose {}}+\operatorname {rem}{\mathopen {}\left (l,m\right )\mathclose {}}\right )\mathclose {}} \\ &= m\cdot \Delta {q} + \Delta {r} \end {aligned}Hence, we have \Delta {r} = {\mathopen {}\left (k - l\right )\mathclose {}} - m \cdot \Delta {q} , and recalling our assumption k\equiv l \mod m we may assume some p such that pm = k-l. Hence \Delta {r} = pm - m\cdot \Delta q = m(p - \Delta q) and therefore m \mid \Delta {r}.
- In the backward direction, we assume \operatorname {rem}{\mathopen {}\left (k,m\right )\mathclose {}} = \operatorname {rem}{\mathopen {}\left (l,m\right )\mathclose {}}, so that in the notation established above we have \Delta {r}=0. Therefore k-l = m\cdot \Delta q and hence k\equiv l\mod m.
Corollary The modular normal form of an integer [01CY]
- October 1, 2026
- Marcelo Fiore, Jon Sterling
Let m be a positive integer. Then:
- For every natural number n, we have n \equiv \operatorname {rem}{\mathopen {}\left (n,m\right )\mathclose {}}\mod m.
- For every integer k there exists a unique integer [k]_m such that 0 \leq [k]_m < m\quad \text {and}\quad k \equiv [k]_m\mod m\text {.}
Proof.
- October 1, 2026
- Marcelo Fiore, Jon Sterling
- By definition, we have n = \operatorname {quo}{\mathopen {}\left (n,m\right )\mathclose {}}\cdot m + \operatorname {rem}{\mathopen {}\left (n,m\right )\mathclose {}} and hence n - \operatorname {rem}{\mathopen {}\left (n,m\right )\mathclose {}} = m\cdot \operatorname {quo}{\mathopen {}\left (n,m\right )\mathclose {}}, so it follows that n \equiv \operatorname {rem}{\mathopen {}\left (n,m\right )\mathclose {}} \mod m.
-
For any integer k, we will establish the existence and uniqueness of [k]_m with the specified property sequentially.
- For existence, we note that k+\lvert k \rvert \cdot m is a natural number congruent to k modulo m (indeed, that it is non-negative can be established by considering the worst case scenario, when m = 1). We now choose [k]_m \coloneqq \operatorname {rem}{\mathopen {}\left (k+\lvert k\rvert \cdot m, m\right )\mathclose {}}\text {.} The reason for choosing k+\lvert k\rvert \cdot m is precisely to get a natural number that is congruent to k modulo m. The first condition 0\leq [k]_m < m holds by definition of remainders. The second condition k \equiv [k]_m\mod m can be seen to hold as follows: \begin {aligned} k &\equiv k + \lvert k\rvert \cdot m &\mod m \\ &\equiv \operatorname {rem}{\mathopen {}\left (k+\lvert k\rvert \cdot m,m\right )\mathclose {}} &\mod m \\ &= [k]_m \end {aligned}
For uniqueness, we will prove the following statement:
For all integers l such that 0\leq l < m and k\equiv l\mod m, we have l = [k]_m.
To that end, let l be an integer such that 0 \leq l < m and k\equiv l\mod m.
- We have l \equiv \operatorname {rem}{\mathopen {}\left (l,m\right )\mathclose {}}\mod m and therefore l = \operatorname {rem}{\mathopen {}\left (l,m\right )\mathclose {}} because 0\leq l < m and because equality of modular integers is congruence.
- We have \operatorname {rem}{\mathopen {}\left (l,m\right )\mathclose {}}=\operatorname {rem}{\mathopen {}\left (k,m\right )\mathclose {}}=\operatorname {rem}{\mathopen {}\left ([k]_m,m\right )\mathclose {}} because l\equiv k \equiv [k]_m\mod m via our characterisation of congruence in terms of remainders.
Hence we have the folowing sequence of equations:
l = \operatorname {rem}{\mathopen {}\left (l,m\right )\mathclose {}} = \operatorname {rem}{\mathopen {}\left (k,m\right )\mathclose {}} = \operatorname {rem}{\mathopen {}\left ([k]_m,m\right )\mathclose {}} = [k]_m
Observation The modular normal form of a natural number [01D1]
- October 8, 2026
- Jon Sterling
We previously showed that the normal form of an intger k modulo m can be expressed as the remainder [k]_m = \operatorname {rem}{\mathopen {}\left (k+\lvert k \rvert \cdot m, m\mathbf {}.\right )\mathclose {}}
When k\geq 0, a simpler characterisation is possible: in that case, we have [k]_m = \operatorname {rem}{\mathopen {}\left (k,m\right )\mathclose {}}.
Proof.
- October 8, 2026
- Jon Sterling
To see that [k]_m=\operatorname {rem}{\mathopen {}\left (k,m\right )\mathclose {}} assuming k\geq 0, it suffices to show that 0 \leq \operatorname {rem}{\mathopen {}\left (k,m\right )\mathclose {}} < m and k \equiv \operatorname {rem}{\mathopen {}\left (k,m\right )\mathclose {}}\mod m. Why is this enough? Because we already showed that these two properties uniquely determine the normal form of k modulo m.
The first property is immediate by definition of remainders. For the second property, is the first observation of Corollary [01CY].
Example/Warning Modular exponentiation [01FA]
- October 31, 2026
- Jon Sterling
Let n:\mathbb {N} be natural number and let k:\mathbb {Z}_m be an integer modulo m. Then the modular exponentiation of k by n is defined to be the normal form {\mathopen {}\left [k^n\right ]\mathclose {}}_m of k^n modulo m.
Warning: modular exponentiation respects congruence in the base, but not the exponent: in other words, even if n\equiv n'\mod m, it need not be the case that k^n \equiv k^{n'} \mod m.