Number Theory

How to Solve Linear Congruences

Follow a clear method for solving ax ≡ b mod m, including when no solution exists and when there are several, with checked examples.

Key takeaways

  • The congruence ax ≡ b (mod m) has a solution exactly when gcd(a, m) divides b.
  • When it is solvable, there are exactly gcd(a, m) different solutions modulo m.
  • If a and m share no factor, a has an inverse modulo m and the solution is unique.
  • The extended Euclidean algorithm finds that inverse quickly, even for large numbers.
  • Dividing a congruence by a number requires adjusting the modulus when the two share a factor.

To solve ax ≡ b (mod m), first compute g, the greatest common divisor of a and m. If g does not divide b, there is no solution. If it does, divide the whole congruence by g, multiply both sides by the inverse of the new coefficient, and list the g solutions that result modulo m.

The rest of this guide explains each of those steps, shows why they work and runs through examples you can check by hand.

What the notation means

We write c ≡ d (mod m) when m divides c − d. Put simply, c and d leave the same remainder on division by m. So 38 ≡ 3 (mod 5). The modulus m is a positive whole number. For a gentler start, see clocks and calendars.

A linear congruence asks for all integers x with ax ≡ b (mod m). If x works, so does x + m, x + 2m and every other number in the same class. So you only need the solutions among 0, 1, ..., m − 1.

For a small modulus you can just try them all. With 3x ≡ 4 (mod 5), the values 3x for x = 0 to 4 leave remainders 0, 3, 1, 4, 2. Only x = 3 gives 4.

The easy case: an inverse exists

An inverse of a modulo m is a number u with a × u ≡ 1 (mod m). It exists exactly when gcd(a, m) = 1. Multiplying both sides of ax ≡ b by u gives x ≡ u × b.

Solve 7x ≡ 3 (mod 10). Since 7 × 3 = 21 ≡ 1, the inverse of 7 is 3.

7x ≡ 3          (mod 10)
3 × 7x ≡ 3 × 3  multiply by the inverse
x ≡ 9
check: 7 × 9 = 63 ≡ 3

The solution is unique modulo 10: x can be 9, 19, 29, −1 and so on.

Notice that you did not "divide by 7". There are no fractions here. Multiplying by an inverse plays the role that division plays in ordinary algebra.

Finding inverses for larger numbers

Guessing fails when the modulus is large. Use the Euclidean algorithm, then work backward, as set out in Bezout's identity and the extended algorithm.

Solve 17x ≡ 5 (mod 43).

43 = 2 × 17 + 9
17 = 1 × 9 + 8
 9 = 1 × 8 + 1        so gcd = 1

Now express 1 in terms of 43 and 17.

1 = 9 − 8
  = 9 − (17 − 9)           = 2 × 9 − 17
  = 2 × (43 − 2 × 17) − 17 = 2 × 43 − 5 × 17

Modulo 43 the term 2 × 43 vanishes, so −5 × 17 ≡ 1. The inverse is −5, which is 38.

x ≡ 38 × 5 = 190 ≡ 18        (190 − 4 × 43)
check: 17 × 18 = 306 ≡ 5     (306 − 7 × 43)

A shortcut: you could use −5 directly, getting x ≡ −25 ≡ 18. Pick whichever arithmetic is easier.

When a and m share a factor

Let g = gcd(a, m). The statement ax ≡ b (mod m) means ax − b = km for some integer k, that is, b = ax − km. Since g divides both a and m, it divides the right side. So g must divide b.

For example, 6x ≡ 4 (mod 9) is impossible. Here g = 3, and 3 does not divide 4. Whatever x you try, 6x leaves remainder 0, 3 or 6.

If g does divide b, divide a, b and m all by g. The new congruence has coprime coefficient and modulus, so it has one solution modulo m/g. That single class splits into g classes modulo m.

An example with six solutions

Solve 12x ≡ 18 (mod 30).

g = gcd(12, 30) = 6,   and 6 divides 18
divide everything by 6:   2x ≡ 3 (mod 5)
inverse of 2 mod 5 is 3:  x ≡ 9 ≡ 4 (mod 5)

Now list every number below 30 that is 4 modulo 5.

x = 4, 9, 14, 19, 24, 29
check x = 9:   12 × 9 = 108 ≡ 18   (108 − 3 × 30)

There are six solutions modulo 30, matching g = 6. Writing only "x = 4" would miss five of them, which is the most common error in this topic.

The method at a glance

Solving ax ≡ b (mod m)
1 Find g = gcd(a, m) Use the Euclidean algorithm 2 Does g divide b? If not, stop: there is no solution 3 Reduce Divide a, b and m by g 4 Invert Multiply by the inverse of the new a 5 List all answers Add the new modulus repeatedly to get g solutions Find g = gcd(a, m) Use the Euclidean algorithm 1 Does g divide b? If not, stop: there is no solution 2 Reduce Divide a, b and m by g 3 Invert Multiply by the inverse of the new a 4 List all answers Add the new modulus repeatedly to get g solutions 5

The rule for cancelling

In ordinary algebra, ax = ay with a ≠ 0 gives x = y. In congruences you must be careful.

If ax ≡ ay (mod m), then x ≡ y (mod m / gcd(a, m)).

Consider 6 × 1 ≡ 6 × 4 (mod 9), since both sides are 6 modulo 9. It would be wrong to conclude 1 ≡ 4 (mod 9). The correct conclusion is 1 ≡ 4 (mod 3), which is true.

A prime modulus removes the problem. Modulo a prime p, every a not divisible by p has an inverse, so cancellation is always allowed. Fermat's little theorem even gives a formula for the inverse: a^(p−2).

Simplifying by hand

Before reaching for the full algorithm, you can often shrink the numbers. Three moves are safe.

  • Replace a or b by anything congruent to it. Modulo 23, the coefficient 21 can become −2.
  • Multiply both sides by a number coprime to the modulus.
  • Cancel a common factor of a and b if that factor is coprime to the modulus.

Take 21x ≡ 8 (mod 23).

21 ≡ −2,  so  −2x ≡ 8
cancel −2, which is coprime to 23:   x ≡ −4 ≡ 19
check: 21 × 19 = 399 = 17 × 23 + 8

Connection with whole-number equations

Solving ax ≡ b (mod m) is the same as solving the equation ax + my = b in integers. Such equations with whole-number unknowns are called Diophantine equations.

Say tickets come in bundles of 12 and 30 and you must reach a total of 18 by buying some bundles and returning others. The equation 12x + 30y = 18 is solved by the example above: x = 4 gives y = −1, since 48 − 30 = 18. Once you have one pair, all the others follow by adding 5 to x and subtracting 2 from y.

Using congruences in a simple cipher

A classroom cipher numbers the letters 0 to 25 and encrypts x as y ≡ 5x + 8 (mod 26). Decryption means solving this linear congruence for x. Since 5 × 21 = 105 ≡ 1 (mod 26), the inverse of 5 is 21.

x ≡ 21 × (y − 8) (mod 26)
letter H is x = 7:   y ≡ 43 ≡ 17
back again:          21 × 9 = 189 ≡ 7

The multiplier must be coprime to 26, or two different letters would encrypt to the same symbol. This is a teaching example only. It is broken easily and should never protect real information.

Why there are exactly g solutions

It is worth proving the count, since the argument is short and explains where the extra answers come from. Write a = gA, b = gB and m = gM. The statement that m divides ax − b says that gM divides g(Ax − B). Cancel the common g: M divides Ax − B. So the original congruence and the reduced one have exactly the same integer solutions.

The reduced congruence has coprime A and M. An inverse of A exists, so its solutions form a single class, say x = x₀ + tM for integers t.

Now ask when two of these values are the same modulo m = gM. The difference (t − s)M is a multiple of gM exactly when g divides t − s. Therefore t = 0, 1, ..., g − 1 give different classes modulo m, and every other t repeats one of them.

Picture a clock with m hours and a hand that jumps a hours at a time. It can only land on multiples of g, which is why b must be one. And the hand completes its circuit after M jumps, so across m jumps it visits each reachable hour g times.

Seeing it as a table

For small moduli, listing the products makes the whole theory visible. Here are the values of 4x modulo 10.

x            0  1  2  3  4  5  6  7  8  9
4x mod 10    0  4  8  2  6  0  4  8  2  6

Only even remainders appear, because gcd(4, 10) = 2. Each one appears exactly twice. So 4x ≡ 8 has the two solutions 2 and 7, while 4x ≡ 3 has none.

Compare 3x modulo 10.

x            0  1  2  3  4  5  6  7  8  9
3x mod 10    0  3  6  9  2  5  8  1  4  7

All ten remainders appear once. Since gcd(3, 10) = 1, every right side is reachable in exactly one way.

A calendar puzzle

Suppose an event repeats every 5 days and today is day 0 of a 7-day week, counted from Sunday. After how many repeats does it first fall on a Wednesday, which is day 3?

You need 5x ≡ 3 (mod 7). The inverse of 5 modulo 7 is 3, because 15 ≡ 1. So x ≡ 9 ≡ 2. After two repeats, 10 days have passed, and day 10 is indeed a Wednesday. Because 5 and 7 are coprime, the event eventually visits every weekday.

If the event repeated every 14 days, then 14x ≡ 0 for every x. It would stay on Sunday forever, and the congruence 14x ≡ 3 would have no solution.

Several congruences at once

Many problems impose more than one condition, such as a remainder of 2 on division by 3 and 3 on division by 5. First solve each linear congruence into the form x ≡ c (mod n). Then combine them using the Chinese remainder theorem.

Mistakes to watch for

Stopping at one solution. When g is larger than 1, remember to list every class modulo the original m. If you prefer a compact answer, writing x ≡ 4 (mod 5) is also complete, as long as you state the smaller modulus.

Dividing without changing the modulus. From 2x ≡ 6 (mod 8) it does not follow that x ≡ 3 (mod 8) only. The value x = 7 works too. The correct step is x ≡ 3 (mod 4).

Multiplying by a number that shares a factor with m. That move can create false solutions. Multiplying 3x ≡ 1 (mod 8) by 2 gives 6x ≡ 2 (mod 8), which x = 7 satisfies although 3 × 7 = 21 ≡ 5. Multiply only by numbers coprime to the modulus.

Mishandling negatives. The remainder of −25 modulo 43 is 18, since −25 + 43 = 18. Add the modulus until the result lands in the usual range.

Skipping the existence check. Computing g first costs little and can save a long, doomed calculation.

Practice problems

Try these, then compare.

congruence answer
4x ≡ 1 (mod 11) x ≡ 3
9x ≡ 6 (mod 15) x ≡ 4, 9, 14
10x ≡ 5 (mod 20) no solution
8x ≡ 12 (mod 28) x ≡ 5, 12, 19, 26

For each, check by substitution. A solution is only as good as the check, and the check costs a single multiplication and a remainder.

Written for 4Mathematics with AI writing tools and not reviewed line by line by a person. Check anything you rely on against a textbook or primary source. Report an error.

Keep reading

All 59 articles in Number Theory →

Latest news on this topic