Is there a formula for solving the congruence equation $ax^2 + bx + c=0$?

If $n = p$ is prime, the situation is straightforward. When $p = 2$ there are a small number of cases, and when $p > 2$ the quadratic formula holds. (Note that the quadratic formula fails when $p = 2$ because you can't divide by $2$. This is because you can't complete the square $\bmod 2$.)

If $n$ is composite, the situation is more complicated. $x$ is a solution if and only if $x$ is a solution $\bmod p^k$ for every prime power factor of $n$ by the Chinese Remainder Theorem, so in particular if, say, $n$ is a product of $k$ distinct primes there can be as many as $2^k$ solutions obtained by combining roots modulo the prime factors of $n$.

After the above step the problem reduces to the prime power case $n = p^k$. In this case the question of what solutions look like is completely answered by Hensel's lemma. Again the case $p = 2$ is special.


The quadratic formula works just as well modulo n as long as $(2a,n) = 1$ and $b^2-4ac$ is a quadratic residue mod n. If either of those conditions do not hold, then there are no solutions.

Edit: as pointed out in the comments, this is not a complete answer; see Qiaochu Yuan's for a much better one.


Here (link) is a thorough discussion of the steps in reducing general moduli quadratic equation problems to those of prime moduli, including the case $p=2$.