# How to solve and get the general solution of a congruence equation

I'm following the tutorial of an online calculator to solve congruence equations

I'm doing the same as they are doing but I never get the same result, I don't know what I'm doing wrong.

$28x\equiv 14 \$mod $6$

$28$ mod $6$ and $14$ mod $6$ I get

a = $4$ and b = $2$

the linear combination of the $gcd(4,6) =$ $4(-1) + (1)6 = 2$

putting into the formula I get

$\begin{equation*} x_0 = \frac{2(-1)}{2} \; ( \text{mod} \; 6) \end{equation*} = 5$

general solution \begin{equation*} x_n = 5 + \frac{n(6)}{2} \; ( \text{mod} \; 6) \end{equation*}

General form of solutions: 2 + 3k.

Solutions for x less than 6: 2,5.

• You cannot generally use division with congruence equations. – Elliot G Aug 22 '17 at 22:41
• you can convert congruences into algebra ( sometimes with multiple variables) see math.stackexchange.com/questions/2401795/… @ElliotG I disagree to some extent, you just have to be very careful. in this case you can reduce to $14x \equiv 7 \bmod 3$ which then converts down to $2x\equiv1\bmod 3$ which then can give you the result. – user451844 Aug 22 '17 at 23:11
• your answer is equivalent. The set all numbers that can be represented as 5+3k is the same set as the set that can be represented 2+3k. But rather than memorizing formulae, you should really be trying to understand the algebra. – Doug M Aug 22 '17 at 23:28
• @DougM You are right, but I don't have much time left to learn such a complex thing, but anyways, thanks. – Goun2 Aug 22 '17 at 23:36
• @hjx it's as easy as 5+3x = 2+3(x+1) – user451844 Aug 22 '17 at 23:39

First of all you can take all the coefficients down by congruence with the modulus.

$$28x\equiv 14 \bmod 6 \quad\to\quad 4x\equiv 2\bmod 6$$

Note that here, in concept, you are not dividing by $7$ - you are taking $28\bmod 6$ and $14\bmod 6$ (even though the effect is the same).

Then you can put aside the common factor of $2$ from coefficients and modulus (although you may need to bring it back for the solution eventually):

$$4x\equiv 2 \bmod 6 \quad\to\quad 2x\equiv 1\bmod 3$$

Now, by examination, $x\equiv 2\bmod 3$ meaning - if you need to present the result $\bmod 6$ - $x\equiv \{2,5\} \bmod 6$, as per your text book.

Rather than a case where you can solve this last step "by examination" you may need to explicitly find the multiplicative inverse (and in this case that is what the equation amounts to anyway). For small moduli this may be an easy search; in general you can use the extended Euclidean algorithm.

• Since your arrows are unidirectional, your deductions are only necessary but not sufficient conditions on solutions. In fact the reverse directions hold too, but you need to explicitly state that if you want to deduce the candidates are actually solutions without having to verify that. – Bill Dubuque Aug 22 '17 at 22:49
• Why doesn't the given formula $\begin{equation*} x_0 = \frac{bp}{\text{gcd}(a, m)} \; ( \text{mod} \; m). \end{equation*}$ give the $x_0$ as 2 ? – Goun2 Aug 22 '17 at 23:00
• @BillDubuque I'm indicating process rather than formal logical linkages with the arrows, which is why I drew them as single-stemmed. But apologies for any confusion. – Joffan Aug 22 '17 at 23:18
• @hjx your formula has $x_{-1}=2$, so that solution is available. You could have got $x_0=2$ by using $2\cdot4 +(-1)\cdot 6 = 2$ for the gcd linear combination – Joffan Aug 22 '17 at 23:22
• Thanks, Could you please explain this part ? Now, unless gcd(a,m) evenly divides b there won't be any solutions to the linear congruence. Though if it does, our first solution is given by – Goun2 Aug 22 '17 at 23:40

$$28x\equiv 14\mod 6$$ means $$6|28x-14$$

Since $28x-14$ is even for every integer $x$, this is equivalent to $$3|28x-14$$ and therefore $$28x\equiv 14\mod 3$$

Reducing $28$ and $14$ mod $3$ gives $$x\equiv 2\mod 3$$

Hence the positive solutions are $2,5,8,\cdots$

• Why doesn't the given formula $\begin{equation*} x_0 = \frac{bp}{\text{gcd}(a, m)} \; ( \text{mod} \; m). \end{equation*}$ give the $x_0$ as 2 ? – Goun2 Aug 22 '17 at 23:01
• What does "p" mean in this formula ? – Peter Aug 22 '17 at 23:08
• $\begin{equation*} ap + mq = \text{gcd}(a, m). \end{equation*}$ – Goun2 Aug 22 '17 at 23:11
• a-calculator.com/congruence/#faq-formula – Goun2 Aug 22 '17 at 23:12
• @hjx so bezout's identity. – user451844 Aug 23 '17 at 0:27

There in no discrepancy:  your solution is $\,x\equiv\, 5,\,5\!+\!3\,\equiv\, 5,\,2\pmod{\!6},\,$ same as claimed.

Remark  Generally, as above, we will need to reduce the results of the formula $\bmod m\,$ if we wish to obtain the least nonnegative representatives of the solutions.

• Bill, in the tutorial the writter says, Now, unless gcd(a,m) evenly divides b there won't be any solutions to the linear congruence. Though if it does, our first solution is given by.. Could you explain what did He meant by saing that ? – Goun2 Aug 23 '17 at 1:37
• in the formula, what is this (mod m) taking mod of ? $\begin{equation*} x_n = x_0 + \frac{nm}{\text{gcd}(a, m)} \; ( \text{mod} \; m) \end{equation*}$ I solved the following congruence $131x\equiv 21 \ (mod \ 77)$ and I got $56 + \frac{77n}{1}$ mod $77$, $77$ mod $77$ would be 0. – Goun2 Aug 23 '17 at 2:01
• @hjx If $\,ax\equiv b\pmod{\!m}\,$ is solvable then $\,b = \color{#c00}ax-\color{#c00}mn\,$ so $\,d\mid \color{#c00}{a,m}\,\Rightarrow\,d\mid b.\,$ Thus choosing $\,d = \gcd(a,m)\,$ we can cancel $\,d\,$ from $\,a,b,m\,$ so reducing to the simpler case where $\,a,m\,$ are coprime, so $\,a^{-1}\!\pmod{\!m}\,$ exists, so $\,x\equiv a^{-1}b\pmod{\!m}.\\$ – Bill Dubuque Aug 23 '17 at 2:03
• @hjx It's not "taking mod". Rather $\,x\equiv y\pmod{\!m}\iff m\mid x-y\iff x,y$ leave the same remainder when divided by $m,\,$ i.e. $\ x\bmod m\, =\, y\bmod m.\,$ For more on "mod" as a binary operator (remainder) vs. ternary congruence relation see this answer and this one. – Bill Dubuque Aug 23 '17 at 2:13
• @hjjx It should be $\,x_0\equiv\,\ldots$ not $\,x_0 =\,\ldots$ since the latter is not even defined. But there is much abuse of notation when it comes to congruences (and much misunderstanding). – Bill Dubuque Aug 23 '17 at 12:37