Skip to main content
Logo image

Section 2.1 The Division Algorithm

Subsection 2.1.1 Statement and examples

Let’s start off with the division algorithm. This is the familiar elementary school fact that if you divide an integer \(a\) by a positive integer \(b\text{,}\) you will always get an integer remainder \(r\) that is nonnegative, but less than \(b\text{.}\)
Equally important, there is only one possible remainder under these circumstances.
Finding \(q\) and \(r\) is easy in small examples like \(a=13,b=3\text{.}\)
\begin{equation*} \text{We have }13=4\cdot 3+1\text{ so }q=4\text{ and }r=1\text{.} \end{equation*}
For bigger values it’s nice to have the result implemented in Sage.
We can check the correctness of the Sage output by multiplying and adding back together.

Sage note 2.1.2. Counting begins at zero.

There are several things to note about this early computation. First, note that the answer to divmod came in parentheses, a so-called tuple data type.
Second, there is another way to approach this computation, more programmatically so that it’s easier to reuse. What do you think the [0] and [1] mean?
To access the first and second parts of the answer (the quotient and remainder), we use square brackets, asking for the 0th and 1st parts of the tuple (9702,18)! (This operation is called indexing.) In Python, the programming language behind Sage (as in many other languages), counting begins at zero.
The discussion in the previous note actually turns out to be an enduring argument in number theory, too. Do we only care about positive numbers, or nonnegative ones as well? We saw this in the stamps example, since one could send a package for free under certain circumstances (campus mail), but might not care about that case. Similarly, are we required to use at least one of each type of stamp, or is it okay (as in our problem) to not use one type?

Subsection 2.1.2 Proof of the Division Algorithm

One neat thing about the division algorithm is that it is not hard to prove but still uses the Well-Ordering Principle; indeed, it depends on it. The key set is the set of all possible remainders of \(a\) when subtracting multiples of \(b\text{,}\) which we call
\begin{equation*} S=\{a-kb \mid k\in\mathbb{Z}\}\text{.} \end{equation*}
(Note that the set looks the same if we add multiples of \(b\text{,}\) since \(k\in\mathbb{Z}\text{,}\) but for the purposes of exposition it is easier to think of it as subtraction.)
The object of main interest in the proof will be the nonnegative piece of \(S\) which we will call \(S'=S\cap \mathbb{N}\text{.}\) For example, if \(a=13,b=3\text{,}\) then \(S=\{\ldots 19, 16, 13, 10, 7, 4, 1, -2, -5, \ldots\}\) while \(S'=\{\ldots 19, 16, 13, 10, 7, 4, 1\}\text{.}\)
Our strategy will be to apply the well-ordering principle to \(S'\text{.}\) (It is worth thinking briefly about why both \(S\) and \(S'\) are nonempty.) Give the name \(r\) to the smallest element of \(S'\text{,}\) which must be writeable as \(r=a-bq\) (that’s the definition of being an element of \(S'\subset S\text{,}\) after all).
Now let’s briefly suppose by way of contradiction that \(r\geq b\text{.}\) In that case we could subtract \(b\) from \(r\text{,}\) and then \(r-b\in S'\) as well. So \(r\) would not be the least element of \(S'\text{,}\) which is a contradiction. Hence we know that \(r<b\text{.}\) (Note that \(r\) is the smallest nonnegative number in \(S'\text{,}\) just as with our intuition regarding remainders from school.)
We still have to show that \(r\) and \(q\) are the only numbers fulfilling this statement. Suppose \(a=bq'+r'\) for some integers \(q',r'\) where \(0\leq r' <b\text{;}\) clearly if \(r=r'\) then we can solve \(a-bq=r=r'=a-bq'\) to get \(q=q'\) (since \(b>0\)), so the only interesting case is if \(r\neq r'\text{.}\) Without loss of generality, we can assume \(r< r'\text{.}\)
In that case, \(a-bq=r< r'=a-bq'\text{,}\) which can be rewritten as \(0< r'-r=b(q-q')\text{.}\) Since \(q,q'\in \mathbb{Z}\text{,}\) by Fact 1.2.2 \(q-q'\) must be at least one if it isn’t zero. But then \(b=b\cdot 1\leq r'-r=b(q-q')\) or \(b\leq r+b\leq r'\text{,}\) which contradicts \(0\leq r' <b\text{.}\) Thus \(q-q'=0\) and hence \(q=q'\) and \(r=r'\text{.}\)
It’s worth actually trying out the details of this proof with some \(a\) and \(b\text{,}\) say with \(a=26\) and \(b=3\text{.}\)
As a scholium (see Exercise 2.5.1) note that if \(b<0\) there can still be a positive remainder, but here we would need \(0\leq r<|b|\) in the theorem.

Subsection 2.1.3 Uses of the division algorithm

It’s kind of fun to prove interesting things about powers using the division algorithm, and likely you did in a previous course. For instance, there is an interesting pattern in the remainders of integers when dividing by 4. If you are online, evaluate the following Sage cell to see the pattern. (It’s also easy to just get the remainders of the first ten or so perfect squares by hand.)

Sage note 2.1.3. Repeating commands for different input.

The syntax for i in [0..10]: just means we want to do the next command for integers from 0 to 10. Such a repetition is called a loop.
Another way Python uses to generate the list of different input is the range command; try substituting range(11) for [0..10] in the Sage cell above. Can you discover what the difference is between these?
The rest of the command (all the percent symbols and so forth) is mostly for correct formatting. That includes the indentation in the second line – an essential part of Python and Sage.
This certainly provides strong numerical evidence for the following proposition. But better than that will be the proof!
Using the division algorithm, we can write \(n=4q+r\text{.}\) What happens if we square it, \((4q+r)^2\text{?}\)
Algebraically this yields \(16q^2+8qr+r^2\text{.}\) Clearly this is a multiple of 4 plus \(r^2\text{.}\) So the only possible remainders of \(n\) are the remainders of \(r^2\text{,}\) where \(r\) is already known to be less than 4!
Now check these yourself to see that the only possibilities are the ones in the statement of the proposition.
One cool thing about this proof is that if we just change the proof from using \(n=(4q+r)^2\) to one using \(n=(mq+r)^2\text{,}\) we can essentially do the same thing for several divisions at once. If the number we divide by is \(m\text{,}\) then
\begin{equation*} (mq+r)^2=m^2q^2+2mqr+r^2=m(mq^2+2qr)+r^2\text{,} \end{equation*}
hence all that matters for the final remainder is \(r^2\text{,}\) since the rest is already divisible by \(m\text{.}\)
But we know that there are only \(m\) possibilities for \(r\text{,}\) so it’s easy to check all their squares. For \(m=6\text{,}\) the following cell checks for you if you don’t want to check them by hand.
This verifies that \(r=0,1,3,4\) are the only possible remainders of perfect squares when you divide by six.