TITLE: Doing modular division when denominator and modulus not coprime
QUESTION [5 upvotes]: So normally if you calculate $n/d \mod m$, you make sure $d$ and $m$ are coprime and then do $n[d]^{-1}\mod m$ , all $\mod m$. But what if $d$ and $m$ are not coprime? What do you do?

REPLY [3 votes]: If $\gcd(m,d)=g$ and $g\mid n$, then you can perform the standard modular division on
$$
\left.\frac{n}{g}\middle/\frac{d}{g}\right.\left(\text{mod}\frac{m}{g}\right)\tag{1}
$$
Note that the division reduces the modulus, too.
The original equation
$$
dx\equiv n\pmod{m}\tag{2}
$$
is equivalent to
$$
dx+my=n\tag{3}
$$
To solve $(3)$, we need to divide through by $g$:
$$
\frac{d}{g}x+\frac{m}{g}y=\frac{n}{g}\tag{4}
$$
and $x$ in $(4)$ is given by $(1)$.
For example, suppose we know that
$$
12x\equiv9\pmod{15}
$$
we would solve
$$
4x\equiv3\pmod{5}
$$
and any solution would only be known mod $5$; that is,
$$
x\equiv2\pmod{5}
$$