Linear Congruences

A linear congruence is an equation of the form ax ≡ b (mod n). It has a solution exactly when d = gcd(a, n) divides b, and then it has d solutions mod n. When gcd(a, n) = 1, multiply both sides by the modular inverse of a, found with the extended Euclidean algorithm, to get the single solution x ≡ a⁻¹b (mod n).

Before this lesson: Modular Arithmetic and Euclidean Algorithm

Why you cannot just divide

In ordinary algebra, 7x = 3 gives x = 3/7. Mod n there are no fractions, so “divide by 7” has to mean “multiply by a number that undoes 7.” That number is a modular inverse.

\bar{a} \text{ is an inverse of } a \text{ mod } n \iff a\bar{a} \equiv 1 \pmod{n}

For example, 3 is an inverse of 7 mod 10, because 7 · 3 = 21 ≡ 1 (mod 10). Some numbers have no inverse: 2x is always even, so 2x ≡ 1 (mod 10) never holds. The rule is that a has an inverse mod n exactly when gcd(a, n) = 1.

\gcd(a, n) = 1

Assume a and n are relatively prime.

ax + ny = 1

Bézout’s identity gives integers x and y (see extended Euclidean algorithm).

ax - 1 = n(-y)

Subtract 1 and ny from both sides.

ax \equiv 1 \pmod{n}

n divides ax − 1, so x is an inverse of a.

Conversely, if ax ≡ 1 (mod n), then ax − 1 = nk, so any common divisor of a and n divides 1. The inverse is unique mod n, and it is written a⁻¹.

How many solutions a linear congruence has

d = \gcd(a, n): \quad ax \equiv b \pmod{n} \text{ has a solution} \iff d \mid b
  • If d ∤ b, there are no solutions, because ax − b = nk would make d divide b.
  • If d = 1, there is exactly one solution mod n: x ≡ a⁻¹b (mod n).
  • If d | b, divide a, b and n by d. The reduced congruence (a/d)x ≡ b/d (mod n/d) has one solution x0 mod n/d, and the original has d solutions mod n: x0, x0 + n/d, …, x0 + (d − 1)n/d.

Steps for solving ax ≡ b (mod n)

  1. Reduce a and b mod n, then find d = gcd(a, n) with the Euclidean algorithm.
  2. If d does not divide b, stop: there is no solution.
  3. If d > 1, divide a, b and n by d.
  4. Find the inverse of the new a with the extended Euclidean algorithm (or by trying small multiples when n is small).
  5. Multiply both sides by the inverse and reduce.
  6. If d > 1, list all d solutions mod n by adding multiples of n/d. Check one solution in the original congruence.

Linear congruence examples

Example 1: 7x ≡ 3 (mod 10)

Solve 7x ≡ 3 (mod 10).

\gcd(7, 10) = 1

7 and 10 share no common factor, so there is exactly one solution mod 10.

7 \cdot 3 = 21 \equiv 1 \pmod{10}

Try small multiples of 7: 3 is an inverse of 7.

3 \cdot 7x \equiv 3 \cdot 3

Multiply both sides by the inverse.

21x \equiv 9

Multiply.

x \equiv 9 \pmod{10}

Reduce 21 ≡ 1.

7 \cdot 9 = 63 \equiv 3

Check: 63 leaves remainder 3 mod 10.

Answer: x ≡ 9 (mod 10), so the solutions are …, −1, 9, 19, 29, ….

Example 2: An inverse from the extended Euclidean algorithm

Solve 17x ≡ 5 (mod 43).

43 = 17 \cdot 2 + 9

Run the Euclidean algorithm on 43 and 17.

17 = 9 \cdot 1 + 8

Divide 17 by 9.

9 = 8 \cdot 1 + 1

Divide 9 by 8. The gcd is 1, so an inverse exists.

1 = 9 - 8

Solve the last division for 1.

1 = 9 - (17 - 9)

Substitute 8 = 17 − 9.

1 = 9 - 17 + 9

Distribute the minus sign.

1 = 2 \cdot 9 - 17

Combine the 9 terms.

1 = 2(43 - 17 \cdot 2) - 17

Substitute 9 = 43 − 17 · 2.

1 = 2 \cdot 43 - 4 \cdot 17 - 17

Distribute the 2.

1 = 2 \cdot 43 - 5 \cdot 17

Combine the 17 terms.

17(-5) \equiv 1 \pmod{43}

Reduce mod 43: the 43 term is a multiple of 43.

17^{-1} \equiv -5 \equiv 38

Add 43 to get the inverse between 0 and 42.

x \equiv 38 \cdot 5 = 190

Multiply both sides of 17x ≡ 5 by 38.

190 = 43 \cdot 4 + 18

Reduce mod 43.

17 \cdot 18 = 306 = 43 \cdot 7 + 5

Check: 306 leaves remainder 5.

Answer: x ≡ 18 (mod 43).

Example 3: A congruence with no solution

Solve 6x ≡ 4 (mod 9).

\gcd(6, 9) = 3

Find the gcd of a and n.

3 \nmid 4

3 does not divide b = 4.

6x - 4 = 9k

Suppose a solution existed, with k an integer.

6x - 9k = 4

Rearrange: move 9k to the left side and 4 to the right side.

3(2x - 3k) = 4

Factor out 3. The left side is a multiple of 3 but 4 is not.

Answer: there is no solution. As a check, 6x mod 9 only takes the values 0, 6 and 3 as x runs through 0, 1, 2, …, 8.

Example 4: A congruence with several solutions

Solve 12x ≡ 18 (mod 30), and list every solution mod 30.

\gcd(12, 30) = 6

Find d.

6 \mid 18

d divides b, so there are exactly 6 solutions mod 30.

2x \equiv 3 \pmod{5}

Divide a, b and n by 6.

2 \cdot 3 = 6 \equiv 1 \pmod{5}

3 is an inverse of 2 mod 5.

x \equiv 3 \cdot 3 = 9

Multiply both sides by 3.

x \equiv 4 \pmod{5}

Reduce mod 5.

x \equiv 4, 9, 14, 19, 24, 29 \pmod{30}

Add multiples of n/d = 5 until there are d = 6 values.

12 \cdot 4 = 48 = 30 + 18

Check one solution in the original congruence.

Answer: x ≡ 4, 9, 14, 19, 24 or 29 (mod 30), which is the same as x ≡ 4 (mod 5).

Two rows of 15 boxes for x = 0 to 29. Each box shows 12x mod 30, and the values repeat 0, 12, 24, 6, 18 every 5 steps. The boxes for x = 4, 9, 14, 19, 24 and 29 contain 18 and are highlighted, so 12x ≡ 18 (mod 30) has exactly 6 solutions, spaced 5 apart.
Since gcd(12, 30) = 6 divides 18, the congruence has 6 solutions mod 30, one in every block of 30/6 = 5.

Example 5: Making change (word problem)

Mugs cost $23 each. A café owner paid only with $10 bills and got $4 in change. She bought fewer than 10 mugs. How many did she buy?

23x + 4 \equiv 0 \pmod{10}

The amount paid, 23x + 4 dollars, is a multiple of 10.

23x \equiv -4 \pmod{10}

Subtract 4 from both sides.

3x \equiv 6 \pmod{10}

Reduce: 23 ≡ 3 and −4 ≡ 6.

3 \cdot 7 = 21 \equiv 1 \pmod{10}

gcd(3, 10) = 1, and 7 is an inverse of 3.

x \equiv 7 \cdot 6 = 42

Multiply both sides by 7.

x \equiv 2 \pmod{10}

Reduce mod 10.

23 \cdot 2 + 4 = 50

Check: 2 mugs cost $46, and $50 in bills leaves $4 change.

Answer: she bought 2 mugs. The other solutions, 12, 22, …, are ruled out because she bought fewer than 10.

Common mistakes

MistakeExample of the errorFix
Writing a fraction7x ≡ 3 (mod 10), so x = 3/7Multiply by the inverse: x ≡ 3 · 3 = 9 (mod 10).
Dividing a and b but not n12x ≡ 18 (mod 30) becomes 2x ≡ 3 (mod 30)Divide the modulus too: 2x ≡ 3 (mod 5).
Giving one solution when there are several12x ≡ 18 (mod 30) has only x ≡ 4List d = 6 solutions mod 30: 4, 9, 14, 19, 24, 29.
Searching for an inverse that does not existTrying every multiple of 6 to solve 6x ≡ 4 (mod 9)Check gcd(a, n) | b first. Here 3 ∤ 4, so stop.
Leaving a negative inverse unreduced17⁻¹ ≡ −5, then reporting x ≡ −25Negative values are correct but reduce them: −5 ≡ 38 and −25 ≡ 18 (mod 43).
Forgetting to reduce a firstSolving 23x ≡ 6 (mod 10) by searching up to 23Reduce 23 ≡ 3 (mod 10) first.

Related ideas

A linear congruence ax ≡ b (mod n) is the same problem as the linear Diophantine equation ax + ny = b, whose solvability test, gcd(a, n) | b, comes from Bézout’s identity. Solving several congruences with different moduli at once is the next lesson, Chinese Remainder Theorem, and RSA Encryption builds its private key as a modular inverse.

Practice problems

  1. Find the inverse of 3 mod 7.
  2. Find the inverse of 11 mod 26.
  3. Solve 5x ≡ 4 (mod 9).
  4. Solve 11x ≡ 7 (mod 26).
  5. Solve 4x ≡ 6 (mod 10). List all solutions mod 10.
  6. Solve 8x ≡ 5 (mod 12).
  7. Solve 9x ≡ 12 (mod 15). List all solutions mod 15.
  8. Use the inverse from Example 2 to solve 17x ≡ 3 (mod 43).
  9. Explain why 6 has no inverse mod 9.
  10. Notebooks cost $9 each. A customer paid only with $20 bills, got $7 in change and bought fewer than 20 notebooks. How many did he buy?
Show answers

1.

3 \cdot 5 = 15

Try multiples of 3 until one is 1 more than a multiple of 7.

15 = 7 \cdot 2 + 1

15 ≡ 1 (mod 7).

Answer: 3⁻¹ ≡ 5 (mod 7)

2.

26 = 11 \cdot 2 + 4

Run the Euclidean algorithm.

11 = 4 \cdot 2 + 3

Divide 11 by 4.

4 = 3 \cdot 1 + 1

Divide 4 by 3. The gcd is 1.

1 = 4 - 3

Solve the last division for 1.

1 = 4 - (11 - 4 \cdot 2)

Substitute 3 = 11 − 4 · 2.

1 = 4 - 11 + 2 \cdot 4

Distribute the minus sign.

1 = 3 \cdot 4 - 11

Combine the 4 terms.

1 = 3(26 - 11 \cdot 2) - 11

Substitute 4 = 26 − 11 · 2.

1 = 3 \cdot 26 - 6 \cdot 11 - 11

Distribute the 3.

1 = 3 \cdot 26 - 7 \cdot 11

Combine the 11 terms.

11(-7) \equiv 1 \pmod{26}

Reduce mod 26.

-7 + 26 = 19

Add 26 to get a value from 0 to 25.

11 \cdot 19 = 209 = 26 \cdot 8 + 1

Check.

Answer: 11⁻¹ ≡ 19 (mod 26)

3.

5 \cdot 2 = 10 \equiv 1 \pmod{9}

gcd(5, 9) = 1, and 2 is an inverse of 5.

x \equiv 2 \cdot 4 = 8

Multiply both sides by 2.

5 \cdot 8 = 40 = 9 \cdot 4 + 4

Check.

Answer: x ≡ 8 (mod 9)

4.

x \equiv 19 \cdot 7 = 133

Multiply both sides by 11⁻¹ ≡ 19 from problem 2.

133 = 26 \cdot 5 + 3

Reduce mod 26.

11 \cdot 3 = 33 = 26 + 7

Check.

Answer: x ≡ 3 (mod 26)

5.

\gcd(4, 10) = 2

Find d.

2 \mid 6

d divides b, so there are 2 solutions mod 10.

2x \equiv 3 \pmod{5}

Divide a, b and n by 2.

x \equiv 3 \cdot 3 = 9

Multiply by 3, the inverse of 2 mod 5.

x \equiv 4 \pmod{5}

Reduce mod 5.

x \equiv 4, 9 \pmod{10}

Add n/d = 5 once.

4 \cdot 4 = 16 \equiv 6,\ 4 \cdot 9 = 36 \equiv 6

Check both mod 10.

Answer: x ≡ 4 or 9 (mod 10)

6.

\gcd(8, 12) = 4

Find d.

4 \nmid 5

d does not divide b.

Answer: no solution

7.

\gcd(9, 15) = 3

Find d.

3 \mid 12

d divides b, so there are 3 solutions mod 15.

3x \equiv 4 \pmod{5}

Divide a, b and n by 3.

3 \cdot 2 = 6 \equiv 1 \pmod{5}

2 is an inverse of 3 mod 5.

x \equiv 2 \cdot 4 = 8 \equiv 3 \pmod{5}

Multiply by 2, then reduce.

x \equiv 3, 8, 13 \pmod{15}

Add multiples of n/d = 5.

9 \cdot 3 = 27 = 15 + 12

Check x = 3.

Answer: x ≡ 3, 8 or 13 (mod 15)

8.

x \equiv 38 \cdot 3 = 114

Multiply both sides by 17⁻¹ ≡ 38.

114 = 43 \cdot 2 + 28

Reduce mod 43.

17 \cdot 28 = 476 = 43 \cdot 11 + 3

Check.

Answer: x ≡ 28 (mod 43)

9.

\gcd(6, 9) = 3

6 and 9 share the factor 3.

6x - 1 = 9k

An inverse would need this for some integers x and k.

3(2x - 3k) = 1

Rearrange and factor. A multiple of 3 cannot equal 1.

Answer: gcd(6, 9) = 3 ≠ 1, so 6 has no inverse mod 9. In fact 6x mod 9 is always 0, 3 or 6

10.

9x + 7 \equiv 0 \pmod{20}

The amount paid, 9x + 7 dollars, is a multiple of 20.

9x \equiv -7 \equiv 13 \pmod{20}

Subtract 7, then add 20 to reduce.

9 \cdot 9 = 81 \equiv 1 \pmod{20}

9 is its own inverse mod 20.

x \equiv 9 \cdot 13 = 117

Multiply both sides by 9.

x \equiv 17 \pmod{20}

Reduce: 117 − 100 = 17.

9 \cdot 17 + 7 = 160 = 8 \cdot 20

Check: $153 + $7 = $160 in $20 bills.

Answer: he bought 17 notebooks

Linear congruences FAQ

How do you solve a linear congruence?

Find d = gcd(a, n). If d does not divide b, there is no solution. Otherwise divide a, b and n by d, multiply by the inverse of the new a, and list the d solutions mod n.

How do you find a modular inverse?

Run the extended Euclidean algorithm on a and n to write ax + ny = 1. Then x mod n is the inverse of a. For small n, trying a, 2a, 3a, … until one is 1 more than a multiple of n is quicker.

How many solutions does ax ≡ b (mod n) have?

None if gcd(a, n) does not divide b. Otherwise exactly gcd(a, n) solutions mod n, spaced n/gcd(a, n) apart.

When does a number have an inverse mod n?

Exactly when gcd(a, n) = 1. So mod a prime p, every number from 1 to p − 1 has an inverse.

Summary

  • A linear congruence ax ≡ b (mod n) is solvable exactly when d = gcd(a, n) divides b.
  • a has an inverse mod n exactly when gcd(a, n) = 1. The extended Euclidean algorithm finds it.
  • When d = 1, the unique solution is x ≡ a⁻¹b (mod n).
  • When d > 1 and d | b, divide a, b and n by d, solve, then list d solutions spaced n/d apart.
  • Always check a solution in the original congruence.

Previous lesson: Modular Arithmetic

Next lesson: Chinese Remainder Theorem

Back to the Discrete Math Index