How to solve a modular arithmetic equation

A linear modular equation takes the form ax≡b(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 ax≡b(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 a′x≡b′(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 a′a' modulo m′m', 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 x≡b′(a′)−1(modm′)x \equiv b'(a')^{-1} \pmod{m'}. 5. Find all dd distinct solutions modulo the original mm by computing xk=x+km′x_k = x + k m' for integers k=0,1,...,d−1k = 0, 1, ..., d-1.

Checking the result

Substitute the derived values of xx back into the original equation ax≡b(modm)ax \equiv b \pmod m. Evaluate ax−bax - 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 14x≡12(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 7x≡6(mod9)7x \equiv 6 \pmod 9. Find the modular inverse of 77 modulo 99. We need a number yy such that 7y≡1(mod9)7y \equiv 1 \pmod 9. Testing values or using the Extended Euclidean Algorithm shows 7(4)=28≡1(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 x≡24(mod9)x \equiv 24 \pmod 9. Reduce 2424 modulo 99 to get x≡6(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 x≡6,15(mod18)x \equiv 6, 15 \pmod{18}.

FAQ

Run your own problem

Drag an image or PDF of the problem here, or click to upload

Ctrl + V to paste

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

See also