Division algorithm

Integer division and remainders (aka The Division Algorithm) Let \(n\) be an integer and \(d\) a positive integer. There are unique integers \(q\) and \(r\), with \(0 \leq r < d\), such that \(n = dq + r\). In this case, \(d\) is called the divisor, \(n\) is called the dividend, \(q\) is called the quotient, and \(r\) is called the remainder.

Because these numbers are guaranteed to exist, the following functions are well-defined:

Because these functions are so important, we sometimes use the notation \(n \textbf{ div } d = \textbf{ div } ( ~(n,d)~)\) and \(n \textbf{ mod } d = \textbf{ mod } (~(n,d)~)\).

Pro-tip: The functions \(\textbf{ div }\) and \(\textbf{ mod }\) are similar to (but not exactly the same as) the operators \(/\) and \(\%\) in Java and python.

Example calculations:

\(20 \textbf{ div } 4\)

\(20 \textbf{ mod } 4\)

\(20 \textbf{ div } 3\)

\(20 \textbf{ mod } 3\)

\(-20 \textbf{ div } 3\)

\(-20 \textbf{ mod } 3\)