Skip to main content
Contents Index
Calc
Dark Mode Prev Next Profile
\(\require{mathtools}\newcommand\DLGray{\color{Gray}}
\newcommand\DLO{\color{BurntOrange}}
\newcommand\DLRa{\color{WildStrawberry}}
\newcommand\DLGa{\color{Green}}
\newcommand\DLGb{\color{PineGreen}}
\newcommand\DLBa{\color{RoyalBlue}}
\newcommand\DLBb{\color{Cerulean}}
\newcommand\ds{\displaystyle}
\newcommand\ddx{\frac{d}{dx}}
\newcommand\os{\overset}
\newcommand\us{\underset}
\newcommand\ob{\overbrace}
\newcommand\obt{\overbracket}
\newcommand\ub{\underbrace}
\newcommand\ubt{\underbracket}
\newcommand\ul{\underline}
\newcommand\tikznode[3][]
{\tikz[remember picture,baseline=(#2.base)]
\node[minimum size=0pt,inner sep=0pt,#1](#2){#3};
}
\newcommand\del{\nabla}
\newcommand\R{\mathbb{R}}
\newcommand\C{\mathcal{C}}
\newcommand\N{\mathcal{N}}
\newcommand\eps{\varepsilon}
\renewcommand\epsilon{\varepsilon}
\renewcommand\subset{\subseteq}
\newcommand\norm[1]{\|{#1}\|}
\newcommand\matrixbrackets[4][1]{
\draw (#3,#2) -- (\fpeval{#3+0.2},#2);
\draw (#3,#1) -- (#3 ,#2);
\draw (#3,#1) -- (\fpeval{#3+0.2},#1);
\draw (#4,#2) -- (\fpeval{#4-0.2},#2);
\draw (#4,#1) -- (#4 ,#2);
\draw (#4,#1) -- (\fpeval{#4-0.2},#1);
}
\tikzstyle{circle node 0}=[fill=white, draw=black, shape=circle, inner sep=0pt]
\tikzstyle{circle node 2}=[fill=white, draw=black, shape=circle, inner sep=2pt]
\tikzstyle{hrect node 0}=[fill=white, draw=black, inner sep=2pt, outer sep=0pt]
\tikzstyle{hrect node 2}=[fill=white, draw=black, inner sep=2pt, outer sep=2pt]
\tikzstyle{hrect node 3}=[fill=white, draw=black, inner sep=2pt, outer sep=3pt]
\tikzstyle{vrect node 0}=[fill=white, draw=black, inner sep=0pt, outer sep=0pt]
\tikzstyle{vrect node 2}=[fill=white, draw=black, inner sep=0pt, outer sep=2pt]
\tikzstyle{vrect node 3}=[fill=white, draw=black, inner sep=0pt, outer sep=3pt]
\tikzstyle{hidden node 0}=[inner sep=0pt, outer sep=0pt]
\tikzstyle{hidden node 2}=[fill=white, inner sep=2pt, outer sep=2pt]
\tikzstyle{rect node 1}=[fill=white, inner sep=2pt, outer sep=0pt]
\tikzstyle{rect node 2}=[fill=white, draw=black, inner sep=2pt, outer sep=0pt]
\newcommand{\lt}{<}
\newcommand{\gt}{>}
\newcommand{\amp}{&}
\definecolor{fillinmathshade}{gray}{0.9}
\newcommand{\fillinmath}[1]{\mathchoice{\colorbox{fillinmathshade}{$\displaystyle \phantom{\,#1\,}$}}{\colorbox{fillinmathshade}{$\textstyle \phantom{\,#1\,}$}}{\colorbox{fillinmathshade}{$\scriptstyle \phantom{\,#1\,}$}}{\colorbox{fillinmathshade}{$\scriptscriptstyle\phantom{\,#1\,}$}}}
\)
Subsection B.1 The mod function
The
mod function computes the
remainder after division. Its syntax is:
mod Syntax.
Returns the remainder
r when
a is divided by
b. The result satisfies
0 <= r < b for positive
b.
In other words,
mod(a, b) answers the question: βafter dividing
a by
b, what is left over?β For example, if you divide 10 by 3 you get 3 with a remainder of 1, so
mod(10, 3) returns
1.
Two of the most common uses for mod are:
Even/odd test
mod(n, 2) returns
0 when
n is even and
1 when
n is odd.
Divisibility test
mod(n, k) == 0 is
true exactly when
n is divisible by
k.
π Example 5 . Basic mod Values.
The table below shows several calls to
mod and their results.
Table 6. Sample mod Results
mod(10, 3)
1
\(10 = 3 \times 3 + 1\)
mod(12, 4)
0
\(12 = 4 \times 3 + 0\)
mod(7, 7)
0
any number mod itself is 0
mod(5, 9)
5
\(5 = 9 \times 0 + 5\)
mod(0, 6)
0
0 divided by anything leaves remainder 0
π Example 7 . Checking Even and Odd Numbers.
Because
mod(n, 2) is
0 for even numbers and
1 for odd numbers, we can use it inside an
if-statement to classify a number:
n = 17;
if mod(n, 2) == 0
fprintf('%d is even\n', n)
else
fprintf('%d is odd\n', n)
end
Running this script prints
17 is odd because
mod(17, 2) equals
1, not
0.
π Example 8 . Printing Multiples with a Loop.
A
for-loop combined with
mod is a natural way to select every
\(k\) -th value from a range. Here we print all multiples of 3 from 1 to 20:
for n = 1:20
if mod(n, 3) == 0
fprintf('%d\n', n)
end
end
The output is
3,
6,
9,
12,
15,
18. The condition
mod(n, 3) == 0 is true exactly when
n is divisible by 3.
Reading Questions Check Your Understanding
1.
(a) mod(9, 3) equals 0.
True.
Because \(9 = 3 \times 3 + 0\text{,}\) the remainder is 0. Any number that divides evenly leaves a remainder of 0.
False.
Because \(9 = 3 \times 3 + 0\text{,}\) the remainder is 0. Any number that divides evenly leaves a remainder of 0.
(b) What does mod(7, 4) return?
What does
mod(7, 4) return?
3
\(7 = 4 \times 1 + 3\text{,}\) so the remainder is 3.
1
1 is the quotient (how many times 4 fits into 7), not the remainder.
4
The remainder is always less than the divisor, so it cannot equal 4 here.
0
0 would mean 7 is divisible by 4, but \(7 / 4\) is not a whole number.
(c) Which expression tests divisibility by 5?
Which expression is
true exactly when
n is divisible by 5?
mod(n, 5) == 0
A remainder of 0 means n divides evenly by 5, which is exactly the definition of divisibility.
mod(5, n) == 0
This tests whether 5 is divisible by n, not whether n is divisible by 5.
mod(n, 5) == 5
The remainder from mod(n, 5) is always between 0 and 4, so it can never equal 5.
n / 5 == 0
This is only true when n is 0, not for all multiples of 5 like 5, 10, or 15.
(d) The result of mod is always less than the divisor.
For a positive divisor
b,
mod(a, b) always returns a value that is strictly less than
b.
True.
By definition, the remainder when dividing by b satisfies \(0 \leq r < b\text{.}\) Once the remainder would reach b, another full group of b can be subtracted, so the remainder resets to 0.
False.
By definition, the remainder when dividing by b satisfies \(0 \leq r < b\text{.}\) Once the remainder would reach b, another full group of b can be subtracted, so the remainder resets to 0.
(e) What does mod(0, 6) return?
What does
mod(0, 6) return?
0
Zero divided by any nonzero number gives a quotient of 0 with remainder 0.
6
The remainder is always less than the divisor, so it cannot be 6.
1
Zero is evenly divisible by 6 (zero times), leaving a remainder of 0, not 1.
undefined
mod(0, b) is well defined and equals 0 for any positive b.
(f) Counting multiples of 4 from 1 to 20.
How many integers from 1 to 20 satisfy
mod(n, 4) == 0?
5
The multiples of 4 in that range are 4, 8, 12, 16, and 20 β five values in all.
4
Donβt forget that 20 is also divisible by 4. The multiples are 4, 8, 12, 16, 20.
6
The next multiple after 20 would be 24, which is outside the range. Count carefully: 4, 8, 12, 16, 20.
3
Three is too few. List them out: \(4, 8, 12, 16, 20\) β that is five values.
(g)
Which expression checks whether
n is even?
mod(n,2) == 0
Even numbers leave remainder 0 when divided by 2.
mod(n,2) == 1
Remainder 1 identifies odd numbers, not even numbers.
n/2 == 0
This is only true when n is zero.
n - 2 == 0
This checks only whether n equals 2.
You have attempted
of
activities on this page.