130. Congruence

130.1 Definition: congruence (mod $k$)

Let $k$ be a fixed positive integer. Two integers $m$ and $n$ are congruent (mod $k$), written “$m\equiv n\pmod{k}$”, if $k$ divides $m-n$, in other words, if there is an integer $q$ for which $m-n=qk$.

130.1.1 Example $9\equiv 3\pmod{6}$, $-5\equiv 16\pmod{7}$, $146\equiv -22\pmod{12}$.

130.1.2 Usage

a) In the phrase “$m\equiv n\pmod{k}$”, $k$ is called the modulus of congruence.

b) The syntax for “mod” here is different from that of the operator “MOD” used in Pascal and other languages. In Pascal, “MOD” is a binary operator like “+”; when used between two variables, as in the phrase “M MOD K”, it causes the calculation of the remainder when M is divided by K. Thus “5 MOD 3”, for example, is an expression (not a statement) having value 2. The phrase “$5\equiv 2\pmod{3}$”, on the other hand, is a sentence that is either true or false.

130.1.3 Exercise List all the positive integers $\leq 100$ that are congruent to 3 mod 24. (Answer on page 250.)

130.1.4 Exercise List all the positive integers $\leq 100$ that are congruent to $-3$ mod 24.

130.1.5 Remark Recall that the remainder when $m$ is divided by $k$ is the unique integer $r$ with $0\leq r<|k|$ for which there is an integer $q$ such that $m=qk+r$. Then we can prove:

130.2 Theorem

Two positive integers $m$ and $n$ are congruent mod $k$ if and only if $m$ and $n$ leave the same remainder when divided by $k$.

Proof If $m=qk+r$ and $n=q'k+r$ (same $r$), then $m-n=(q-q')k$, so $k$ divides $m-n$. Then by definition $m\equiv n\pmod{k}$.

Conversely, if $m\equiv n\pmod{k}$, let $r$ be the remainder when $m$ is divided by $k$ and $r'$ the remainder when $n$ is divided by $k$. Then there are quotients $q$ and $q'$ for which $m=qk+r$ and $n=q'k+r'$. Then $r-r'=(m-qk)-(n-q'k)=m-n+(q'-q)k$. Since $m-n$ is divisible by $k$, this means $r-r'$ is divisible by $k$. Since $r$ and $r'$ are both between 0 and $k$ (not including $k$), this means $r=r'$, as required.

130.3 Theorem

Congruence $\pmod{k}$ is an equivalence relation.

Proof Here is the proof that it is transitive; the rest is left to you. Suppose that $m\equiv n\pmod{k}$ and $n\equiv p\pmod{k}$. Then $m$ leaves the same remainder as $n$ when divided by $k$, and $n$ leaves the same remainder as $p$ when divided by $k$. Since remainders are unique, $m$ leaves the same remainder as $p$ when divided by $k$, so, by Theorem 130.2 $m\equiv p\pmod{k}$.

Congruence has an important special property connected with addition and multiplication that has given it extensive applications in computer science:

130.4 Theorem

If $m\equiv m'\pmod{k}$ and $n\equiv n'\pmod{k}$ then $m+n\equiv m'+n'\pmod{k}$ and $mn\equiv m'n'\pmod{k}$.

Proof The hypothesis translates into the statement

$$k\mid m-m'\text{ and }k\mid n-n'$$

Then $(m+n)-(m'+n')=m-m'+n-n'$ is the sum of two numbers divisible by $k$, so is divisible by $k$. Hence $m+n\equiv m'+n'\pmod{k}$. Also $mn-m'n'=mn-mn'+mn'-m'n'=m(n-n')+n'(m-m')$, again the sum of two numbers divisible by $k$, so that $mn\equiv m'n'\pmod{k}$.

130.4.1 Remark The consequence of Theorem 130.4 is that if you have an expression involving integers, addition and multiplication, you can freely substitute integers congruent to the integers you replace and the expression will evaluate to an integer that, although it may be different, will be congruent $\pmod{k}$ to the original value.

130.4.2 Example As an example, what is $5^8$ congruent to $\pmod{16}$? The arithmetic is much simplified if you reduce each time you multiply by 5:

$$\begin{aligned} 5 &\equiv 5 \pmod{16} \\ 5^2 &\equiv 25\equiv 9 \pmod{16} \\ 5^3 &\equiv 5\cdot 9\equiv 45\equiv 13 \pmod{16} \\ 5^4 &\equiv 5\cdot 13\equiv 65\equiv 1 \pmod{16} \\ 5^8 &\equiv (5^4)^2\equiv 1^2\equiv 1 \pmod{16} \end{aligned} \tag{130.1}$$

130.4.3 Remark This ability to compute powers fast is the basis of an important technique in cryptography.

130.4.4 Exercise Compute:

a) $5^{12}\pmod{4}$

b) $5^{12}\pmod{10}$

c) $5^{12}\pmod{16}$ (Answer on page 250.)

130.4.5 Exercise Prove that if $s\mid t$, then

$$ms\equiv ns\pmod{t}\Leftrightarrow m\equiv n\pmod{t/s}$$

Pages 201–203 of the book (PDF).