Section 3.2 Division Algorithm
In Definition 1.3.10 we had defined multiplication as repeated addition. For positive integers we conducted division as repeated subtraction. We first consider this case and then generalize the algorithm to all integers by giving a division algorithm for negative integers.
Watch the video in Figure 3.2.1 on the Division algorithm and then read the detailed description in the remainder of this section.
Subsection 3.2.1 Division Algorithm for positive integers
In our first version of the division algorithm we start with a nonnegative integer \(a\) and keep subtracting a natural number \(b\) until we end up with a number that is less than \(b\) and greater than or equal to \(0\text{.}\) We call the number of times that we can subtract \(b\) from \(a\) the quotient of the division of \(a\) by \(b\text{.}\) The remaining number is called the remainder of the division of \(a\) by \(b\text{.}\)
We typically use the variable \(q\) for the quotient and the variable \(r\) for the remainder. We have
The division algorithm computes the quotient as well as the remainder. In Algorithm 3.2.2 and Algorithm 3.2.10 we indicate this by giving two values separated by a comma after the return.
If \(a\lt b\) then we cannot subtract \(b\) from \(a\) and end up with a number greater than or equal to \(b\text{.}\) Thus, in this case, the quotient is 0 and the remainder is \(a\) itself. We catch this case in step 1 of the algorithm.
Algorithm 3.2.2. Division for positive numbers.
 Input:
a natural number \(a\) and a natural number \(b\)
 Output:
Two integers \(q\) and \(r\) such that \(a=(q\cdot b)+r\) and \(0 \leq r\lt b\)
if \(a\lt b\) then return \(0,a\)
let \(q:=0\)
let \(r:=a\)

repeat
let \(q:=q+1\)
let \(r:=rb\)
until \(r\lt b\)
return \(q,r\)
We first consider an example in which the algorithm terminates before we enter the repeat_until loop.
Example 3.2.3. Dividing \(4\) by \(7\) with Algorithm 3.2.2.
We find the output values of Algorithm 3.2.2 for the input values \(a=4\) and \(b=7\text{.}\)
Input: \(a=4\) and \(b=7\)
As \(a=4\) and \(b=7\) statement \(a \lt b\) is true. So we follow the instruction after then and return the values of \(q\) and \(r\text{,}\) namely 0 and 4.
Output: \(0\text{,}\)\(4\)
Thus the quotient of the division of \(4\) by \(7\) is \(0\) and the remainder is \(4\text{.}\)
With the division algorithm we find a quotient and remainder. In this example we go through the repeat_until loop several times.
Example 3.2.4. Dividing \(30\) by \(8\) with Algorithm 3.2.2.
We find the output values of the Algorithm 3.2.2 for the input values \(a=30\) and \(b=8\text{.}\)
Input: \(a=30\) and \(b=8\)
1. As \(a=30\) and \(b=8\) the statement \(a \lt b\) is false. So we continue with step 2.
2. let \(q:=0\)
3. let \(r:=30\)
4.a. let \(q:=0+1=1\)
4.b. let \(r:=308=22\)
5. As \(r=22\) and \(q=1\) the statement \(r \lt q\) is false. So we continue with step 4
4.a. let \(q:=1+1=2\)
4.b let \(r:=228=14\)
5. As \(r=14\) and \(q=8\) the statement \(r \lt q\) is false. So we continue with step 4
4.a. let \(q:=1+2=3\)
4.b let \(r:=148=6\)
As \(r=6\) and \(q=8\) the statement \(r\lt q\) is true. So we continue with step 6.
We return the quotient \(q=3\) and the remainder \(r=6\)
Output: \(3\text{,}\) \(6\)
Thus the quotient of the division of \(30\) by \(8\) is \(3\) and the remainder is \(6\text{.}\)
When working through the instructions of Algorithm 3.2.2 it can be more convenient to give the values of all relevant variables in each iteration of the loop in a table. We revisit Example 3.2.4 present the work in a more compact form.
Example 3.2.5. Dividing \(30\) by \(8\) with Algorithm 3.2.2 shorter.
We find the output values of Algorithm 3.2.2 for the input values \(a=30\) and \(b=8\text{.}\)
In each row of the table we write the values of all variables for an iteration of the loop. If a variable has no value we leave the entry blank. Similarly in for the output we leave the entries of all variables that are not part of the output blank.
steps  \(a\)  \(b\)  \(q\)  \(r\) 

Input  \(30\)  \(8\)  \(\)  \(\) 
1.,2.,3.  \(30\)  \(8\)  \(0\)  \(30\) 
4.,5.  \(30\)  \(8\)  \(0+1=1\)  \(308=22\) 
4.,5.  \(30\)  \(8\)  \(1+1=2\)  \(228=14\) 
4.,5.  \(30\)  \(8\)  \(1+1=3\)  \(148=6\) 
Output  \(\)  \(\)  \(3\)  \(6\) 
So the output is \(q=3\) and \(r=6\)
In Example 3.2.6 you can observe how the values of the variables change as you click your way through the steps of the division algorithm.
Example 3.2.6. Division algorithm positive interactive.
In Checkpoint 3.2.7 we unroll the loop in Algorithm 3.2.2 in a similar way. Follow the instructions to find quotient and remainder.
Checkpoint 3.2.7. Find quotient and remainder with division algorithm.
Division
Let \(a:=94\) and \(b:=28\text{.}\) With the division algorithm find \(a\;\mathrm{div}\;b\) and \(r = a\bmod b\text{.}\)
Input: \(a :=\) and \(b:=\) .
let \(q:=0\) and let \(r:=a =\) .
\(\quad\) let \(q:=q+1=\) and let \(r:= r28=\) .
\(\quad\) let \(q:=q+1=\) and let \(r:= r28=\) .
\(\quad\) let \(q:=q+1=\) and let \(r:= r28=\) .
Because now \(r\lt b\) the loop ends here.
Output: The quotient \(q=\) and the remainder \(r=\) .
Sometimes one is not interested in both the quotient and the remainder. In such a case one can use a simplified algorithm. Determine the output of the algorithm in Checkpoint 3.2.8.
Checkpoint 3.2.8. Another variant of the division algorithm.
Consider the algorithm:
Algorithm
Input: A natural number \(n\) and a natural number \(m\)
(1) if \(n\lt m\) then return \(n\)
(2) repeat
 (a) let \(n:=nm,\)
(3) until \(n\lt m\)
(4) return \(n\)
What does the algorithm return when the input is \(n:=14\) and \(m:=7\) ?
What does the algorithm return when the input is \(n:=9\) and \(m:=7\) ?
What does the algorithm return when the input is \(n:=14\) and \(m:=7\) ?
What does the algorithm compute ?
\(\displaystyle m\cdot n\)
\(\displaystyle (m)^n\)
The remainder of the division of \(n\) by \(m\text{.}\)
\(\displaystyle n+m\)
The difference of the sum of the first \(n\) natural numbers and \(m\text{.}\)
The quotient of the division of \(n\) by \(m\text{.}\)
If \(a>0\text{,}\) then Algorithm 3.2.2 returns the quotient and remainder of the division of \(a\) by \(b\text{.}\) If we try to use Algorithm 3.2.2 when \(a\) is negative, the algorithm always returns \(0,a\) which does not satisfy the condition \(0\le r\) for the output since \(r=a\lt 0\text{.}\) So we need a different algorithm for the case \(a\lt 0\text{.}\)
Subsection 3.2.2 Division Algorithm for Negative Integers
When \(a\lt 0\text{,}\) we still want find \(q\) and \(r\) such that \(a=(q\cdot b)+r\) with \(0\le r\lt b\text{.}\) We get a positive remainder when \(a\) is negative by repeated addition of \(b\text{.}\) This is the same as repeatedly adding \(b\text{.}\) Let \(s\) be the number of times we have to add \(b\) to \(a\) in order to get \(0 \le r \lt b\text{.}\) After \(s\) additions of \(b\) to \(a\) we have
If we let \(q:=s\text{,}\) we get \(r=a(q\cdot b)\) (compare this to what we wanted). We stop when \(0\le r\lt b\text{.}\) We repeatedly add \(b\) to negative numbers until \(0\le r\lt b\) is true. Since a negative number plus \(b\) is always less than \(b\) and we check the value of \(r\) after every addition, it is sufficient to check whether \(0\le r\text{.}\)
Example 3.2.9. Dividing \(33\) by \(9\).
We illustrate the process of dividing a negative number by dividing \(33\) by \(9\text{.}\) We repeatedly add \(9\) until we get a number from \(0\) to \(91=8\text{.}\) That number is the remainder. The negative of the number of times we add \(9\) is the quotient.
As \(0\le 3\lt 9\) we are done. The remainder is \(3\text{.}\) We have added \(9\) four times, so the quotient is \(4\text{.}\) We have
We now formalize this procedure in an algorithm.
Algorithm 3.2.10. Division for negative integers.
 Input:
A negative integer \(a\) and a natural number \(b\)
 Output:
Two integers \(q\) and \(r\) such that \(a=(q\cdot b)+r\) and \(0 \leq r\lt b\)
let \(q:=0\)
let \(r:=a\)

repeat
let \(q:=q1\)
let \(r:=r+b\)
until \(r\ge 0\)
return \(q,r\)
Example 3.2.11. \(20\fdiv 7\) and \(20\fmod 7\) with Algorithm 3.2.10.
We find the output values of the Division Algorithm (Algorithm 3.2.10) for the input values \(a=20\) and \(b=7\text{.}\)
Input: \(a=20\) and \(b=7\)
1. let \(q:=0\)
2. let \(r:=a=20\)
3.
3.a. let \(q:=01=1\)
3.b. let \(r:=20+7=13\)
4. As \(r=20\) the statement \(r\ge 0\) is false. So we continue with step 3.
3.a. let \(q:=11=2\)
3.b. let \(r:=13+7=6\)
4. As \(r=6\) the statement \(r\ge 0\) is false. So we continue with step 3.
3.a. let \(q:=21=3\)
3.b. let \(r:=6+7=1\)
4. As \(r=1\) the statement \(r\ge 0\) is false. So we continue with step 5.
5. We return the quotient \(q=3\) and the remainder \(r=1\)
Output: \(3\text{,}\) \(1\)
Thus the quotient or the division of \(20\) by \(7\) is \(3\) and the remainder is \(1\text{.}\)
In Example 3.2.12 you can observe how the values of the variables change as you click your way thought the steps of the division algorithm.
Example 3.2.12. Division algorithm negative interactive.
In Checkpoint 3.2.13 we unroll the loop in Algorithm 3.2.2. Follow the instructions to find quotient and remainder.
Checkpoint 3.2.13. Find quotient and remainder with division algorithm.
Division
Let \(a:=41\) and \(b:=12\text{.}\) With the division algorithm find \(a\;\mathrm{div}\;b\) and \(r = a\bmod b\text{.}\)
Input: \(a :=\) and \(b:=\) .
let \(q:=0\) and let \(r:=a =\) .
\(\quad\) let \(q:=q1=\) and let \(r:=r + 12=\) .
\(\quad\) let \(q:=q1=\) and let \(r:=r + 12=\) .
\(\quad\) let \(q:=q1=\) and let \(r:=r + 12=\) .
\(\quad\) let \(q:=q1=\) and let \(r:=r + 12=\) .
Because now \(r>0\) the loop ends here.
Output: The quotient \(q=\) and the remainder \(r=\) .
\(41\)
\(12\)
\(41\)
\(1\)
\(29\)
\(2\)
\(17\)
\(3\)
\(5\)
\(4\)
\(7\)
\(4\)
\(7\)
We give some more examples.
Example 3.2.14. Previous result written as \(a=(q\cdot b)+r\).

In Example 3.2.3 we have seen that when dividing \(a=4\) by \(b=7\) the quotient is \(q=0\) and the remainder is \(r=4\text{.}\) Thus we have:
\begin{equation*} 4=(7 \cdot 0) + 4 \end{equation*} 
In Example 3.2.5 we have seen that when dividing \(a=30\) by \(b=8\) the quotient is \(q=3\) and the remainder is \(r=6\text{.}\) Thus we have:
\begin{equation*} 30=(3 \cdot 8) + 6 \end{equation*} 
In Example 3.2.5 we have seen that when dividing \(a=20\) by \(b=7\) the quotient is \(q=3\) and the remainder is \(r=1\text{.}\) Thus we have:
\begin{equation*} 20=(7 \cdot (3)) + 1 \end{equation*}
Problem 3.2.15. Write as \(a=(q\cdot b)+r\).
For the given values of \(a\) and \(b\text{,}\) determine the quotient \(q\) and remainder \(r\) of the division of \(a\) by \(b\text{,}\) and write the equality \(a=(q \cdot b) + r\text{.}\)
\(a=7\) and \(b=3\)
\(a=7\) and \(b=8\)
\(a=20\) and \(b=4\)
\(a=13\) and \(b=3\)
The answers are provided here, but details for the solution are omitted.
For \(a=7\) and \(b=3\text{,}\) we have \(q=2\) and \(r=1\text{,}\) and write \(7=(3\cdot 2)+1\text{.}\)
For \(a=7\) and \(b=8\text{,}\) we have \(q=0\) and \(r=7\text{,}\) and write \(7=(8\cdot 0)+7\text{.}\)
For \(a=20\) and \(b=4\text{,}\) we have \(q=5\) and \(r=0\text{,}\) and write \(20=(4\cdot 5)+0\text{.}\)
For \(a=13\) and \(b=3\text{,}\) we have \(q=5\) and \(r=2\text{,}\) and write \(13=(3\cdot (5)) +2\text{.}\)
Using Algorithm 3.2.2 or Algorithm 3.2.10, we can compute the quotient and remainder of the division of any integer \(a\) by any natural number \(b\text{.}\) For \(a=0\) and any natural number \(b\) we have \(a=(q\cdot b)+r\) and \(0\le r\lt b\) when \(q=0\) and \(r=0\text{.}\)
Thus for all integers \(a\) and all natural numbers \(b\) we can find integers \(q\) and \(r\) such that \(a=(q\cdot b)+r\) and \(0\le r\lt b\text{.}\) We call the combination of the two algorithms the division algorithm.
Subsection 3.2.3 \(\fdiv\) and \(\fmod\)
The construction of \(q\) and \(r\) in Algorithm 3.2.2 and Algorithm 3.2.10 yields a proof of the following theorem.
Theorem 3.2.16.
Let \(a\) be an integer and \(b\) be a natural number. Then, there exist unique integers \(q\) and \(r\) with \(0 \leq r \lt b\) such that
Next we introduce notation for the the quotient and remainder of the division of an integer by a natural number.
Definition 3.2.17.
Let \(a\) be an integer and \(b\) be a natural number, and let \(q\) and \(r\) be the unique integers such that \(0 \leq r \lt b\) and \(a=(q\cdot b)+r\text{.}\)
We call \(q\) the quotient of the division of \(a\) by \(b\) and denote it by \(a\fdiv b\text{.}\)
We call \(r\) the remainder of the division of \(a\) by \(b\) and denote it by \(a\fmod b\text{.}\)
We rewrite the quotient and remainder from some of the previous examples with \(\fdiv\) and \(\fmod\text{.}\)
Example 3.2.18. Previous results written with \(\fdiv\) and \(\fmod\).

In Example 3.2.3 we have seen that when dividing \(a=4\) by \(b=7\) the quotient is \(0\) and the remainder is \(4\text{.}\) Thus \(4=0\cdot 7+4\text{.}\) We write:
\(4 \fdiv 7 = 0\) and \(4\fmod 7=4\)

In Example 3.2.5 we have seen that when dividing \(a=30\) by \(b=8\) the quotient is \(q=3\) and the remainder is \(r=6\text{.}\) Thus \(30=3\cdot 8+6\text{.}\) We write:
\(30 \fdiv 8 = 3\) and \(30\fmod 8 = 6\)

In Example 3.2.5 we have seen that when dividing \(a=20\) by \(b=7\) the quotient is \(3\) and the remainder is \(1\text{.}\) Thus \(20=(3)\cdot 7+1\text{.}\) We write:
\(20 \fdiv 7 = 3\) and \(20\fmod 7=1\)
In the Checkpoint 3.2.19 write the quotient and remainder with the new operations \(\fdiv\) and \(\fmod\text{.}\)
Checkpoint 3.2.19. \(a \fdiv b\) and \(a \fmod b\) from \(a=q\cdot b+r\).
When we are given the quotient and remainder from the division of an integer \(a\) by a natural number \(b\text{,}\) we can recover \(a\text{.}\)
Problem 3.2.20. \(a\) from \(a \fdiv b\) and \(a \fmod b\).
Assume \(a\) is the integer such that
and
What is \(a\) ?
By Theorem 3.2.16 we know that \(a\) can be written in the form
In Definition 3.2.17 we introduced the notation
and
Replacing \(q\) and \(r\) by these expressions we get
We are given exactly these two values, in particular
and
Thus we can replace \(a \fdiv 42\) by \(10\) and \(a \fmod 42\) by \(7\) in the equation above and get
Evaluating the expression we get
In Checkpoint 3.2.21 use conduct the computation from the example above to find the integer \(a\) when given \(a \fdiv b\) and \(a \fmod b\text{.}\)