publicclassSeconds{finalstaticintSEC_IN_YEAR=364*24*60*60;publicstaticvoidmain(Stringargs[]){System.out.println("Number of seconds in 10100 years: "+(SEC_IN_YEAR*10100));}}
$javac Test.java
$java -Xmx128M -Xms16M Test
Number of seconds in 10100 years: -186619904
You’ll recognize this as “integer overflow.” It happens because computers use modular arithmetic
to operate on finite integer data types, such as int.
It all starts with divisibility …
Divisibility is the core concept behind modular arithmetic.
Definition: $a$ divides $b$, written as $a \vert b$.
For $a\in\Z, b\in\Z$, $a \vert b \leftrightarrow \exists k\in\Z. b = ka$.
Examples: which of the following are true and which are false?
$5 \vert 1$
$\color{deeppink}{\mathsf{F}}$
$1 \vert 5$
$\color{green}{\mathsf{T}}$
$25 \vert 5$
$\color{deeppink}{\mathsf{F}}$
$5 \vert 25$
$\color{green}{\mathsf{T}}$
$5 \vert 0$
$\color{green}{\mathsf{T}}$
$0 \vert 5$
$\color{deeppink}{\mathsf{F}}$
$2 \vert 3$
$\color{deeppink}{\mathsf{F}}$
$3 \vert 2$
$\color{deeppink}{\mathsf{F}}$
Division theorem
Division theorem
For $a\in\Z, d\in\Z$ with $d > 0$,
there exist unique integers $q, r$ with $0 \leq r < d$
such that $a = dq+r$.
If we divide $a$ by $d$, we get a unique quotient $q = \div{a}{d}$
and non-negative remainder $r = \mod{a}{d}$.
Note that $r\geq 0$ even if $a < 0$, so mod is not %.
publicclassNotMod{publicstaticvoidmain(Stringargs[]){System.out.println("-5 mod 2 = 1.");System.out.println("-5 % 2 = "+(-5%2));}}
Definition: $a$ is congruent to $b$ modulo $m$, written as $\congruent{a}{b}{m}$
For $a,b,m\in\Z$ with $m>0$, $\congruent{a}{b}{m} \leftrightarrow m \vert (a-b)$
Examples: what do these mean and when are they true?
$\congruent{x}{0}{2}$
True for every $x$ that is divisible by 2, i.e., even.
$\congruent{-1}{19}{5}$
True because $-1-19=-20$ is divisible by 5.
$\congruent{y}{2}{7}$
True for every $y$ of the form $y=2+7k$ where $k\in\Z$.
Congruence and equality
Congruence property
Let $a, b, m \in \Z$ with $m>0$.
Then, $\congruent{a}{b}{m}$ if and only if $\mod{a}{m} = \mod{b}{m}$.
Proof:
Suppose that $\congruent{a}{b}{m}$.
Then $m \vert a-b$ by definition of congruence.
So $a-b = km$ for some $k\in\Z$ by definition of divides.
Therefore, $a = b + km$.
Taking both sides modulo $m$, we get $\mod{a}{m}=\mod{(b+km)}{m}=\mod{b}{m}.$
Suppose that $\mod{a}{m} = \mod{b}{m}$.
By the division theorem, $a = mq + (\mod{a}{m})$ and $b = ms + (\mod{b}{m})$ for some $q,s\in\Z$.
Then,
$a - b$ $=$
$(mq + (\mod{a}{m})) - (ms + (\mod{b}{m}))$ $=$
$m(q-s) + (\mod{a}{m}-\mod{b}{m})$ $=$ $m(q-s)$,
since $\mod{a}{m} = \mod{b}{m}$.
Therefore, $m \vert (a-b)$ and so $\congruent{a}{b}{m}$.
The $\mod{}{m}\ $ function vs the $\congruent{}{\!}{m}$ predicate
The $\mod{}{m}$ function takes any $a\in\Z$ and
maps it to a remainder $\mod{a}{m}\in\{0, 1, \ldots, m-1\}$.
In other words, $\mod{}{m}$ places all integers
that have the same remainder modulo $m$ into the same “group”
(a.k.a. “congruence class”).
The $\congruent{}{\!}{m}$ predicate compares $a,b\in\Z$ and
returns true if and only if $a$ and $b$ are in the same group
according to the $\mod{}{m}$ function.
Modular addition property
Modular addition property
Let $m$ be a positive integer ($m \in \Z$ with $m>0$).
If $\congruent{a}{b}{m}$ and $\congruent{c}{d}{m}$, then $\congruent{a+c}{b+d}{m}$.
Proof:
Suppose that $\congruent{a}{b}{m}$ and $\congruent{c}{d}{m}$.
By the definition of congruence, there are $k$ and $j$
such that $a - b = km$ and $c - d = jm$.
Adding these equations together, we get $(a+c)-(b+d) = m(j+k)$.
Reapplying the definition of congruence, we get that $\congruent{(a+c)}{(b+d)}{m}$.
Modular multiplication property
Modular multiplication property
Let $m$ be a positive integer ($m \in \Z$ with $m>0$).
If $\congruent{a}{b}{m}$ and $\congruent{c}{d}{m}$, then $\congruent{ac}{bd}{m}$.
Proof:
Suppose that $\congruent{a}{b}{m}$ and $\congruent{c}{d}{m}$.
By the definition of congruence, there are $k$ and $j$
such that $a - b = km$ and $c - d = jm$.
So, $a = km + b$ and $c = jm + b$.
Multiplying these equations together, we get $ac = (km + b)(jm + d) = kjm^2 + kmd + bjm + bd$.
Rearranging gives us $ac - bd = m(kjm + kd + bj)$.
Reapplying the definition of congruence, we get that $\congruent{ac}{bd}{m}$.
Example: a proof using modular arithmetic
Let $n\in\Z$, and prove that $\congruent{n^2}{0}{4}$ or $\congruent{n^2}{1}{4}$.
Suppose $\congruent{n}{0}{2}$.
Then $n=2k$ for some integer $k$.
So $n^2 = (2k)^2 = 4k^2$.
Therefore, by definition of congruence, $\congruent{n^2}{0}{4}$.
Case 2 ($n$ is odd):
Suppose $\congruent{n}{1}{2}$.
Then $n=2k+1$ for some integer $k$.
So $n^2 = (2k+1)^2 = 4k^2 + 4k + 1$ $=$ $4(k^2 + k) + 1$.
Therefore, by definition of congruence, $\congruent{n^2}{1}{4}$.
Modular arithmetic and integer representations
Unsigned, sign-magnitude, and two’s complement representation.
Unsigned integer representation
Represent integer $x$ as a sum of $n$ powers of 2:
If $x = \sum_{i=0}^{n-1} b_i2^i$ where each $b_i\in\{0,1\}$,
then the representation is $b_{n-1}\ldots b_2 b_1 b_0$.
Examples:
$99 = 64 + 32 + 2 + 1$
$18 = 16 + 2$
So for $n = 8$:
$99 = 0110\ 0011$
$18 = 0001\ 0010$
This works for unsigned integers.
How do we represented signed integers?
Sign-magnitude integer representation
If $-2^{n-1} < x < 2^{n-1}$, represent $x$ with $n$ bits as follows:
Use the first bit as the sign (0 for positive and 1 for negative), and
the remaining $n-1$ bits as the (unsigned) value.
Examples:
$99 = 64 + 32 + 2 + 1$
$18 = 16 + 2$
So for $n = 8$:
$\ \ \,99 = 0110\ 0011$
$-18 = 1001\ 0010$
$\ \ \,81 = 0101\ 0001$
The problem with this representation is that our standard
arithmetic algorithms no longer work, e.g., adding the
representation of -18 and 99 doesn’t give the representation of 81.
Two’s complement integer representation
Represent $x$ with $n$ bits as follows:
If $0 \leq x < 2^{n-1}$, use the $n$-bit unsigned representation of $x$.
If $-2^{n-1} \leq x < 0$, use the $n$-bit unsigned representation of $2^n - |x|$.
Key property:
Two’s complement representation of any number $y$
is equivalent to $\mod{y}{2^n}$ so arithmetic works $\mod{}{2^n}$.
For $-2^{n-1} \leq x < 0$, $x$ is represented using the $n$-bit unsigned representation of $2^n - |x|$.
Here is an easy way to compute this value:
Compute the $n$-bit unsigned representation of $|x|$.
Flip the bits of $|x|$ to get the representation of $2^n-1-|x|$.
This works because the string of all 1’s represents $2^n-1$.
Add 1 to get $2^n - |x|$.
Applications of modular arithmetic
Hashing, pseudo-random numbers, ciphers.
Hashing
Problem:
We want to map a small number of data values from a large domain
$\{0, 1, \ldots, M-1\}$ into a small set of locations
$\{0, 1, \ldots, n-1\}$ to be able to quickly check if a value is present.
Solution:
Compute $\text{hash}(x) = \mod{x}{p}$ for a prime $p$ close to $n$.
Or, compute $\text{hash}(x) = \mod{ax+b}{p}$ for a prime $p$ close to $n$.
This approach depends on all of the bits of data the data.
Helps avoid collisions due to similar values.
But need to manage them if they occur.
Pseudo-random number generation
Linear Congruential method
$x_{n+1} = \mod{(ax_n + c)}{m}$
Choose random $x_0,a,c,m$ and produce a long sequences of $x_n$’s.
Simple ciphers
Ceasar or shift cipher:
Treat letters as numbers: A = 0, B = 1, …
$f(p) = \mod{(p + k)}{26}$
$f^{-1}(p) = \mod{(p - k)}{26}$
More general version:
$f(p) = \mod{(ap + b)}{26}$
$f^{-1}(p) = \mod{(a^{-1}(p - b))}{26}$
Summary
Sets can be represented efficiently using bitvectors.
This representation is used heavily in the real world.
With this representation, set operations reduce to fast bitwise operations.
Modular arithmetic is arithmetic over a finite domain.
Key notions are divisibility and congruency $\mod{}{m}$.
Modular arithmetic is the basis of computing.
Used with two’s complement representation to implement computer arithmetic.
Also used in hashing, pseudo-random number generation, and cryptography.