How to solve a modular arithmetic equation

A linear modular equation takes the form axb(modm)ax \equiv b \pmod m. It is solved by finding the greatest common divisor of aa and mm, reducing the equation, and multiplying by the modular inverse of the reduced coefficient.

The setup

Identify the coefficients aa, bb, and the modulus mm in the equation axb(modm)ax \equiv b \pmod m. Calculate the greatest common divisor (GCD) of aa and mm, denoted as d=gcd(a,m)d = \gcd(a, m).

The steps

  1. Verify a solution exists: dd must evenly divide bb. If it does not, there are no solutions. 2. Divide the entire equation, including the modulus, by dd to yield axb(modm)a'x \equiv b' \pmod{m'}, where a=a/da' = a/d, b=b/db' = b/d, and m=m/dm' = m/d. Note that gcd(a,m)=1\gcd(a', m') = 1. 3. Find the modular multiplicative inverse of aa' modulo mm', denoted (a)1(a')^{-1}, using the Extended Euclidean Algorithm. 4. Multiply both sides of the reduced equation by (a)1(a')^{-1} to isolate xx, yielding xb(a)1(modm)x \equiv b'(a')^{-1} \pmod{m'}. 5. Find all dd distinct solutions modulo the original mm by computing xk=x+kmx_k = x + k m' for integers k=0,1,...,d1k = 0, 1, ..., d-1.

Checking the result

Substitute the derived values of xx back into the original equation axb(modm)ax \equiv b \pmod m. Evaluate axbax - b. The result must be an integer multiple of mm.

Common errors

Forgetting to divide the modulus mm by the GCD dd during the reduction step is the most frequent mistake. Another common error is attempting to find a modular inverse for aa modulo mm when gcd(a,m)eq1\gcd(a, m) eq 1, which is mathematically undefined.

Worked example

Solve 14x12(mod18)14x \equiv 12 \pmod{18}.

Identify variables: a=14a = 14, b=12b = 12, m=18m = 18. Calculate d=gcd(14,18)=2d = \gcd(14, 18) = 2. Since 22 divides 1212, exactly 22 solutions exist modulo 1818. Divide the entire equation by 22 to get 7x6(mod9)7x \equiv 6 \pmod 9. Find the modular inverse of 77 modulo 99. We need a number yy such that 7y1(mod9)7y \equiv 1 \pmod 9. Testing values or using the Extended Euclidean Algorithm shows 7(4)=281(mod9)7(4) = 28 \equiv 1 \pmod 9. The inverse is 44. Multiply both sides of the reduced equation by 44: 4(7x)4(6)(mod9)4(7x) \equiv 4(6) \pmod 9. This simplifies to x24(mod9)x \equiv 24 \pmod 9. Reduce 2424 modulo 99 to get x6(mod9)x \equiv 6 \pmod 9. Generate the d=2d=2 solutions modulo the original modulus 1818. x0=6+0(9)=6x_0 = 6 + 0(9) = 6. x1=6+1(9)=15x_1 = 6 + 1(9) = 15. The solutions are x6,15(mod18)x \equiv 6, 15 \pmod{18}.

FAQ

Run your own problem

References: Kenneth H. Rosen, Discrete Mathematics and Its Applications · OpenStax, Contemporary Mathematics, Chapter 9: Number Theory

See also