Solving $ax \equiv c \pmod b$ efficiently when $a,b$ are not coprime

Solving $154x \equiv 14 \pmod{182}$ is the same as finding all solutions to $$ 154x + 182y = 14.$$ In this case, we might think of this as finding all solutions to $$14(11x + 13y) = 14(1),$$ or rather $$11x + 13 y = 1.$$ Finally, solving this is the same as solving $11x \equiv 1 \pmod {13}$, which has solution $x \equiv 6 \pmod{13}$.

So we learn that $x \equiv 6 \pmod{13}$ is the solution. Of course, this isn't a single residue class mod $182$. Thinking modulo $182$, we see that the solutions are $x \equiv 6, 6+13,6+26,6+39, \ldots, 6+13*13 \equiv 6, 19, 32, \ldots, 175.$

This approach works generally --- factor out the greatest common divisor, consider the resulting modular problem, and then bring it back up to the original problem.


Note $\ \gcd( 154,182)=\color{#c00}{14}\,$ so factoring it out and cancelling it yields

$$ \color{#c00}{14}\cdot 13\,\mid\, \color{#c00}{14}\,(11x\!-\!1)\!\!\overset{\rm\ \ cancel\ \color{#c00}{14}_{\phantom{I_I}}\!\!\!\!}\iff\ 13\mid 11x\!-\!1\iff {\rm mod}\ \ 13\!:\ x\equiv \dfrac{1}{11}\equiv \dfrac{-12}{-2}\equiv 6\qquad $$

Below I derive the general solution in fractional form, which often greatly simplifies matters. Then I show how to present the extended Euclidean algorithm succinctly using these (multi-valued) modular fractions. See my other answer above for how this method applies to the OP.


Generally let's consider the solution of $\ B\, x \equiv A\pmod{\! M}.\ $ If $\,d=(B,M)\,$ then $\, d\mid B,\,\ d\mid M\mid B\,x\!-\!A\,\Rightarrow\, d\mid A\ $ is a necessary condition for a solution $\,x\,$ to exist.

If so let $\ m, a, b \, =\, M/d,\, A/d,\, B/d.\ $ Cancelling $\,d\,$ $\rm\color{darkorange}{everywhere}$ i.e. from $\,A,B\,$ & $M$ yields

$$\ x\equiv \dfrac{A}B\!\!\!\pmod{\!M}\iff M\mid B\,x\!-\!A \!\!\overset{\rm\large\ \, cancel \ d}\iff\, m\mid b\,x\! -\! a \iff x\equiv \dfrac{a}b\!\!\!\pmod{\!m}\qquad$$

where the fraction $\ x\equiv a/b\pmod{\! m}\,$ denotes all solutions of $\,ax\equiv b\pmod{\! m},\, $ and similarly for the fraction $\ x\equiv A/B\pmod{\! M}.\ $ Note there may be zero, one, or multiple solutions.

The above implies that if solutions exist then we can compute them by cancelling $\,d = (B,M)\,$ $\rm\color{darkorange}{everywhere},$ i.e. from the numerator $A,\,$ the denominator $B,\,$ $\rm\color{darkorange}{and}$ the modulus $M,\,$ i.e.

$$ x\equiv \dfrac{ad}{bd}\!\!\!\pmod{\! md}\iff x\equiv \dfrac{a}b\!\!\!\pmod{\! m}\qquad $$

where $\bmod m\!:\ a/b = ab^{-1}\,$ uniquely exists as $\,b^{-1}\,$ does, by $\,(b,m)=1$.

If $\, d>1\, $ the fraction $\, x\equiv A/B\pmod{\!M}\,$ is multiple-valued, denoting the $\,d\,$ solutions

$$\quad\ \begin{align} x \equiv a/b\!\!\pmod{\! m}\, &\equiv\, \{\, a/b + k\,m\}_{\,\large 0\le k<d}\!\!\pmod{\!M},\,\ M = md\\[.3em] &\equiv\, \{a/b,\,\ a/b\! +\! m,\,\ldots,\, a/b\! +\! (d\!-\!1)m\}\!\!\pmod{\! M} \end{align}$$

which is true because $\ km\bmod dm =\, (\color{#c00}{k\bmod d})\, m\ $ by the mod Distributive Law, $ $ and the RHS takes exactly $\,d\,$ values, namely $\,\color{#c00}0m,\, \color{#c00}1m,\, \color{#c00}2m, \ldots, (\color{#c00}{d\!-\!1})m,\, $ so ditto for their shifts by $\,a/b,\,$ e.g.

$$\overbrace{\dfrac{6}3\!\!\!\!\pmod{\!12}}^{{\rm\large cancel}\ \ \Large (3,12)\,=\,3}\!\!\!\!\equiv \dfrac{2}{1}\!\!\!\!\pmod{\!4}\,\equiv\, \!\!\!\!\!\!\!\!\!\!\!\!\!\overbrace{\{2,6,10\}}^{\qquad\ \ \Large\{ 2\,+\,4k\}_{\ \Large 0\le k< 3}}\!\!\!\!\!\!\!\!\!\!\!\!\!\!\!\!\pmod{\!12}\qquad $$


Remark $ $ Such multiple-valued fractions frequently arise in the extended Euclidean algorithm when performed in fractional form. Let's use it to compute $\, x\equiv \color{#0a0}{9/5}\pmod{\!18}.\,$ We obtain

$${\rm mod}\ 18\!:\ \ \ \underbrace{\overbrace{\dfrac{0}{18}\overset{\large\frown}\equiv \color{#0a0}{\dfrac{9}5} \overset{\large\frown}\equiv \dfrac{9}3}^{\Large\ \ 0\,-\,3(\color{#0a0}9)\ \equiv\ 9\ }}_{\Large 18\,-\,3(\color{#0a0}5)\ \equiv\ 3}\overset{\large\frown}\equiv \dfrac{0}{2}\overset{\large\frown}\equiv \color{#c00}{\dfrac{9}{1}}\overset{\large\frown}\equiv\dfrac{0}0\qquad\quad $$

so $\ {\rm mod}\ 18\!:\ x\equiv\color{#0a0}{9/5}\equiv\color{#c00}{ 9/1}\equiv 9.\,$ Checking $\, 5x\equiv 5\cdot9\equiv 45\equiv 9,\,$ is indeed true. Expressed equivalently in terms of equations (congruences) the above becomes (e.g. also short & long)

$$ \begin{align} \bmod 18\!:\ \ \ \ \ \ \ \ [\![1]\!]\ \ \ \ 18\, x&\,\equiv \ 0\ \\ [\![2]\!] \ \ \ \ \ \ \color{#0a0}{5\,x}&\ \color{#0a0}{ \equiv\ 9}\!\!\!\\ [\![1]\!]-3\,[\![2]\!] \rightarrow [\![3]\!]\ \ \ \ \ \ \color{0a0}{3\,x} &\ \color{0a0}{\equiv\ 9}\ \\ [\![2]\!]\ \:\!-\:\!\ [\![3]\!] \rightarrow [\![4]\!] \ \ \ \ \ \ \color{90f}{2\,x}&\ \color{90f}{ \equiv\ 0}\\ [\![3]\!] \:\!\ - \:\!\ [\![4]\!] \rightarrow [\![5]\!] \ \ \ \ \ \ \color{#c00}{1\,x}&\ \color{#c00}{ \equiv\ 9} \end{align}\qquad\qquad\qquad\qquad$$

Above each Euclidean reduction step essentially mods out successive denominators as follows

$$ \dfrac{a}{b}\overset{\large\frown}\equiv\dfrac{c}d\overset{\large\frown}\equiv\dfrac{a-qc}{b-qd}\ \ {\rm where}\ \ q = \lfloor b/d \rfloor,\ \ {\rm so }\ \ b\!-\!qd = b\bmod d$$

i.e. the denominators are the values occurring in Euclid's algorithm for $\,\gcd(18,\color{#0a0}5),\,$ but we perform those operations in parallel on the numerators too, e.g. the first step above has $\, q =\lfloor 18/\color{#0a0}5\rfloor = 3\,$ thus the denominator is $\, 18-3(\color{#0a0}5)\equiv 3.\,$ Executing the same operation on the numerators yields the next numerator, $ $ viz. $\,\ 0-3(\color{#0a0}9)\equiv 9.\,$ The following steps proceed the same way, but all quotients (except final $\,q=2)$ are $\,q=1,\,$ so we simply subtract successive numerators and denominators.

The invariant in the algorithm is that the common solutions of each neighboring pair of fractions remains constant. It starts as the common solution of $\,0/18\overset{\large\frown}\equiv 9/5$ $\,:= 18x\equiv 0,\ 5x\equiv 9.\,$ which is equivalent to $\,5x\equiv 9,\,$ since $\,18x\equiv 0\,$ is true for all $\,x\,$ by $\,18\equiv 0.\,$ Similarly it ends with the common solution of $\,9/1 \overset{\large\frown}\equiv 0/0\,$ $:= 1x\equiv 9,\ 0x\equiv 0,\,$ and again the latter can deleted.

The proof that the Euclidean reduction preserves the solution set is as follows.

$\qquad\ \ $ If $\,\ \color{#0a0}{dx\!-\!c \equiv 0}\,\ $ then $\,\ bx\!-\!a \equiv 0\! \iff\! \underbrace{(bx\!-\!a)-q(\color{#0a0}{dx\!-\!c})}_{\Large (b-qd)\,x\,-\,(a-qc)}\!\equiv 0$

This immediately implies that $\ \ \begin{align}bx&\equiv a\\ dx&\equiv c\end{align}$ $\!\iff\!\! \begin{align}(b\!-\!qd)x&\equiv a\!-\!qc\\ dx&\equiv c\end{align}$

It is instructive to look at the intermediate system $\, 9/3\overset{\large\frown}\equiv 0/2.\,$ By above we know that

$$\begin{align} &\overbrace{\dfrac{9}3\!\!\!\pmod{\!18}}^{{\rm\large cancel}\ \ \Large (3,18)\,=\,3}\!\!\!\equiv\, \dfrac{3}{1}\!\!\!\pmod{\!6}\,\equiv\, \{3,\color{#c00}9,15\}\!\!\!\pmod{\!18} \\[.7em] & \underbrace{\dfrac{0}2\!\!\!\pmod{\!18}}_{{\rm\large cancel}\ \ \Large (2,18)\,=\,2}\!\!\!\equiv\, \dfrac{0}{1}\!\!\!\pmod{\!9}\,\equiv\, \{0,\color{#c00}9\}\ \ \ \pmod{\!18} \end{align}\quad\ \ $$

Notice that the common solution of both is indeed $\,\ x\equiv \color{#c00}9\pmod{\!18},\, $ as we found above. Note also that even though we started with a fraction $\,9/5\,$ whose denominator $\,5\,$ is coprime to the modulus $\,18\,$ (so the fraction is single-valued), the Euclidean algorithm passes through various multiple-valued fractions (with non-coprime denominators), even systems with both fractions multiple-valued, such as $\, 9/3\overset{\large\frown}\equiv 0/2\,$ above, i.e. the system $\, 3x\equiv 9,\ 2x\equiv 0\pmod{\!18}.$

The chosen notation $\,\large \frac{a}b \overset{\frown}\equiv \frac{c}d\,$ resembles a padlock (and a congruence combined with intersection) in order to emphasize that the fractions are locked together via intersection - generally we cannot separate the fractions - rather, the solution is the intersection of the adjacent multivalued fractions, so it is not necessarily equal to either one of them (as in the example above). More generally when we have more than two fractions (or equations) this can be done by keeping track of which fractions (or equations) have been eliminated (above it is all but the two most recent).

Such calculations are more commonly expressed without fractions by instead performing operations on systems of equations (as in above example) - operations generalizing Gaussian elimination and triangularization, e.g. reduction of matrices to Hermite /Smith normal form. These topics are studied more abstractly in the theory of modules in abstract algebra (essentially generalizing linear algebra to allow scalars from a ring, not only a field).


To solve $ax\equiv c \mod b$, set $\;d=a\wedge b$, $\;a=a'd, \;b=b'd$. This congruence implies $c$ is divisible by $d$. Actually, it's easy to see that $$ax\equiv c\mod b\iff \begin{cases}c\equiv 0\mod a\wedge b\\\text{and}\\a'x\equiv c'=\dfrac{c}{a\wedge b} \mod b' \end{cases}$$ Thus the problem comes down to the case $a$ and $b$ coprime, after a compatibility condition has been checked.

Added: solution of the second congruence

First we check with the Euclidean algorithm that $\gcd(12260,24755)=5$, and $$\frac{12260}5=2452,\quad\frac{24755}5=4951,\quad\frac{24560}5=4912. $$ Thus the given congruence is equivalent to $ \; 2452 x\equiv 4912\mod 4951$, and we have to find the inverse of $2452$ modulo $4951$. This means we have to find a *Bézout's relation between $2452$ and $4951$. It can be obtained with the extended Euclidean algorithm: $$\begin{array}{rrrr} r_i&u_i&v_i&q_i\\ \hline 4951&0&1\\ 2452&1&0&2\\\hline 47&-2&1&52\\ 8&105&-52&5\\ 7&-527&261&1\\ 1&632&-313\\\hline \end{array}$$ Thus $632\cdot2452-313\cdot4951=1$, whence $2452^{-1}=632\bmod4951$, and the solution is $$x\equiv 632\cdot4912\equiv 107\mod4951.$$