0

$$x = 4 \bmod 18$$

$$x = 52 \bmod 96$$

$$x = 6 \bmod 20$$

My current algorithm thinks the answer is $x \equiv 1066 \bmod 1440$ but I don't think there should be a solution to this.

The algorithm:

function solve_congruences(values, moduli):
    u = first value in values
    M = first modulus in moduli
    u = u mod M //just in case

    for each v,N pair from values and moduli:
        v = v mod N
        if u==v:
            M = lcm(M,N)
            skip the rest of this loop (next pair)
        gcdmn = gcd(M,N)
        if u mod gcdmn  isn't equal to v mod gcdmn :
            return NO SOLUTION
        g,a,b = egcd(M/gcdmn , -N/gcdmn ) //extended gcd
        newmod = lcm(M,N)
        u = (u+a*M) mod newmod 
        M = newmod
    return u,M

2 Answers2

2

We will work these two at a time.


Note that $(18,96)=6$ and $4\equiv52\pmod{6}$, so the first two equations are solvable. We need to solve $$ \frac{x-4}{6}\equiv\begin{bmatrix}0\\8\end{bmatrix}\text{mod}\begin{bmatrix}3\\16\end{bmatrix}\tag{1} $$ Using the Extended Euclidean Algorithm as implemented in this answer, we get $$ \begin{array}{r} &&5&3\\\hline 1&0&1&-3\\ 0&1&-5&16\\ 16&3&1&0 \end{array}\tag{2} $$ which says that $$ 16(1)+3(-5)=1\tag{3} $$ which tells us that $$ 16\equiv\begin{bmatrix}1\\0\end{bmatrix}\text{mod}\begin{bmatrix}3\\16\end{bmatrix}\tag{4} $$ and $$ -15\equiv\begin{bmatrix}0\\1\end{bmatrix}\text{mod}\begin{bmatrix}3\\16\end{bmatrix}\tag{5} $$ If we add $0$ times $(4)$ to $8$ times $(5)$ we get $$ -120\equiv\begin{bmatrix}0\\8\end{bmatrix}\text{mod}\begin{bmatrix}3\\16\end{bmatrix}\tag{6} $$ which solves $(1)$. Therefore, $\frac{x-4}{6}\equiv-120\pmod{48}$, so that $$ \begin{align} x &\equiv-716\pmod{288}\\ &\equiv148\pmod{288}\tag{7} \end{align} $$


Next we need to solve $(7)$ and the third equation from the question. The GCD of the moduli is $(288,20)=4$, but $6\not\equiv148\pmod{4}$, so the third equation can not be solved with the first two.

robjohn
  • 345,667
0

You are correct, there is no solution since the final two congruences are inconsistent when reduced mod the gcd of their moduli $\, = 4 = \gcd(96,20),\,$ becoming $\,x \equiv 0,\ x\equiv 2\pmod{\!4}\,$ resp.

Generally a system of congruences is solvable $\iff$ pairwise solvable.

Bill Dubuque
  • 272,048