Number TheoryCongruences

Congruence mod n

a ≡ b (mod n) when n ∣ (a − b), that is, when a and b leave the same remainder on division by n.

Not started Jump to practice

Clocks, calendars and odd/even all ignore multiples of some fixed number: 15 hours after 11 o'clock is 2 o'clock, because only the remainder mod 12 matters. Gauss turned this into an algebra with one new symbol, and it is the language of the rest of this course.

Definition. Let $n \ge 1$. Integers $a$ and $b$ are congruent mod $n$, written $$a \equiv b \pmod{n},$$ when $n \mid (a - b)$. The number $n$ is the modulus. For example $38 \equiv 3 \pmod{7}$, because $38 - 3 = 35 = 7 \cdot 5$.

Why it works

Congruent means "same remainder". Use the division algorithm: $a = nq + r$ and $b = nq' + r'$ with $0 \le r, r' < n$. Then $a - b = n(q - q') + (r - r')$, so $n \mid (a - b)$ exactly when $n \mid (r - r')$. But $r - r'$ lies strictly between $-n$ and $n$, and the only multiple of $n$ there is $0$. So $a \equiv b \pmod{n}$ exactly when $r = r'$.

Congruence mod $n$ is an equivalence relation: $a \equiv a$; if $a \equiv b$ then $b \equiv a$; if $a \equiv b$ and $b \equiv c$ then $a \equiv c$ (add $a - b$ and $b - c$). Its classes, the residue classes, are the $n$ sets of integers with remainder $0, 1, \dots, n - 1$. The least nonnegative residue of $a$ is its remainder, the one representative from $0$ to $n - 1$.

Mod 7 the number line wraps around a clock with 7 points. Going back 3 from 0 lands on 4, so $-3 \equiv 4 \pmod{7}$; the gray labels outside show the negative names of each point.

Worked example: Least nonnegative residues

Find the least nonnegative residues of $38$ and $-3$ mod $7$.
  1. $38 = 7 \cdot 5 + 3$, so $38 \equiv 3 \pmod{7}$.
  2. $-3 = 7 \cdot (-1) + 4$, so $-3 \equiv 4 \pmod{7}$.
  3. Many numbers name the same class: $38 \equiv 3 \equiv 10 \equiv -4 \pmod{7}$.

Answer: $3$ and $4$.

Worked example: Which moduli work?

For which $n > 1$ is $41 \equiv 17 \pmod{n}$?
  1. We need $n \mid 41 - 17 = 24$.
  2. The divisors of $24$ above $1$: $2, 3, 4, 6, 8, 12, 24$.

Answer: $n = 2, 3, 4, 6, 8, 12$ or $24$.

Worked example: Odd squares mod 8

Show that the square of an odd number is $\equiv 1 \pmod{8}$, using classes mod 8.
  1. An odd number is $\equiv 1, 3, 5$ or $7 \pmod{8}$.
  2. $1^{2} = 1$, $3^{2} = 9 \equiv 1$, $5^{2} = 25 \equiv 1$, $7^{2} = 49 \equiv 1 \pmod{8}$.
  3. So every odd square is $\equiv 1 \pmod{8}$ (squares of congruent numbers are congruent, as the next lesson shows).

Answer: Always $1$.

Common mistake

Dropping the sign: "$-3 \equiv 3 \pmod{7}$." Test it with the definition: $-3 - 3 = -6$, and $7 \nmid -6$. On the clock, $-3$ is three steps back from 0, which is the point $4$: $-3 \equiv 4 \pmod{7}$. To reduce a negative number, add multiples of $n$ until you land between $0$ and $n - 1$.

Key idea

$a \equiv b \pmod{n}$ means $n \mid (a - b)$, which is the same as "$a$ and $b$ leave the same remainder." Every integer is congruent to exactly one of $0, 1, \dots, n - 1$.

Practice

Pass with 3 right answers, the last 2 in a row. Each right answer earns 1 XP.

Loading practice…

Builds on

Leads to

Sources and further reading

  • William Stein, Elementary Number Theory: Primes, Congruences, and Secrets (Springer, 2009; free online edition), chapter 2, The Ring of Integers Modulo n (congruences modulo n)
  • Lehman, Leighton and Meyer, Mathematics for Computer Science (MIT OpenCourseWare 6.042J), chapter on number theory: modular arithmetic
@teddyrookbook GitHub