1

$$\eqalign{ x &\equiv 5 \mod 15\cr x &\equiv 8 \mod 21\cr}$$

The extended Euclidean algorithm gives $x≡50 \bmod 105$.

I understand now that if we combine the two it implies $15a-21b = 3$ but I don't understand how to use the extended GCD to go from there to finding $x$ and the corresponding modulus.

This is what I am using for the extended gcd computations:

def egcd(a, b):
    if a == 0:
        return (b, 0, 1)
    else:
        g, y, x = egcd(b % a, a)
        return (g, x - (b // a) * y, y)

From https://en.wikibooks.org/wiki/Algorithm_Implementation/Mathematics/Extended_Euclidean_algorithm#Python

2 Answers2

2

Since the GCD of the moduli is $(15,21)=3$, it is necessary that $x$ be the same thing in both equations mod $3$. That is, $$ x\equiv5\pmod{15}\implies x\equiv2\pmod{3} $$ and $$ x\equiv8\pmod{21}\implies x\equiv2\pmod{3} $$ If we didn't get that $x\equiv2\pmod{3}$ from both equations, a solution would not be possible.

This prompts us to look at $\frac{x-2}3\pmod{\frac{15}3}$ and $\frac{x-2}3\pmod{\frac{21}3}$. That is, $$ \frac{x-2}3\equiv1\pmod{5}\tag{1} $$ and $$ \frac{x-2}3\equiv2\pmod{7}\tag{2} $$

Using the Extended Euclidean Algorithm as implemented in this answer, $$ \begin{array}{r} &&1&2&2\\\hline 1&0&1&-2&5\\ 0&1&-1&3&-7\\ 7&5&2&1&0 \end{array}\tag{3} $$ we get that $$ \underbrace{5(3)}_{\large\color{#C00000}{15}}+\underbrace{\!7(-2)\!}_{\large\color{#00A000}{-14}}=1\tag{4} $$ We can use $(4)$ to see that $$ \begin{align} \color{#00A000}{-14}&\equiv\color{#0000F0}{1}\pmod{5}\\ \color{#00A000}{-14}&\equiv\color{#0000F0}{0}\pmod{7} \end{align}\tag{5} $$ and that $$ \begin{align} \color{#C00000}{15}&\equiv\color{#0000F0}{0}\pmod{5}\\ \color{#C00000}{15}&\equiv\color{#0000F0}{1}\pmod{7} \end{align}\tag{6} $$ To solve $(1)$ and $(2)$ we can add $1$ times $(5)$ to $2$ times $(6)$ to get $$ \begin{align} 16&\equiv\color{#0000F0}{1}\pmod{5}\\ 16&\equiv\color{#0000F0}{2}\pmod{7} \end{align}\tag{7} $$ Equations $(7)$ tell us that $\frac{x-2}3\equiv16\pmod{35}$ or that $$ \bbox[5px,border:2px solid #C0A000]{x\equiv50\pmod{105}}\tag{8} $$

robjohn
  • 345,667
  • Replacing $x$ with $(x-2)/3$ is likely too clever and unmotivated for the OP's interests. The OP wants a systematic method. Also, the OP should have linked to their previous question. – anon Jul 29 '15 at 03:10
  • @anon: Actually, the process behind getting $(1)$ and $(2)$ is almost necessary in order to solve the problem. Perhaps what I need to do is to motivate what I did to get those lines. Editing... – robjohn Jul 29 '15 at 03:17
  • How did you get the righthand 1 in (x-2)/3 = 1 mod 5, and how did you get the righthand 2 in (x-2)/3 = 2 mod 7? – user2597879 Jul 29 '15 at 03:49
  • @user2597879: $\frac{5-2}3=1$ for the mod $5$ equation and $\frac{8-2}{3}=2$ for the mod $7$ equation. Note that if $x\equiv ab\pmod{am}$, then $a\mid x$ and $\frac xa\equiv b\pmod{m}$. – robjohn Jul 29 '15 at 03:59
  • @user2597879: no. Take a look at the end of my last comment. Since $x-2\equiv 3\pmod{15}$, we know that $3\mid x-2$, so the division is actual division, not modular division. – robjohn Jul 29 '15 at 04:06
  • How did you go from (4) to (5) and (6)? – user2597879 Jul 29 '15 at 04:07
  • (I ask because I am writing a program to do this and I just want to make sure I have a systematic understanding of what's going on. So far I'm good up to (4)) – user2597879 Jul 29 '15 at 04:10
  • @user2597879: I have added color to show where the $-14$ and $15$ come from. – robjohn Jul 29 '15 at 04:16
  • Is there a systematic way to get to (6)? I see how how those four congruences came about, but how do you know to multiply the first by 1 and the second by two (I think the goal is to get the same left term, but aren't there several?)? And is there ever any need to do another GCD check like you did at the start at this point? – user2597879 Jul 29 '15 at 04:18
  • Also it seems like the 1 and 2 never really get used anywhere else from steps (1) and (2). Once you've done the gcd check it looks like you can reduce to moduli 5, 7 and then run the extended gcd algorithm with 5 and 7 as arguments to get gcd=1 and a,b=3, -2. – user2597879 Jul 29 '15 at 04:23
  • I assume you are asking about $(7)$ and not $(6)$. In $(7)$ we are trying to find $\frac{x-2}3$ so that $\frac{x-2}3\equiv1\pmod{5}$ and $\frac{x-2}3\equiv2\pmod{7}$ because that is what we have from $(1)$ and $(2)$. We get those by taking linear combinations of $(5)$ and $(6)$. – robjohn Jul 29 '15 at 04:26
  • Yes, but is there a systematic way or is it freehanded? – user2597879 Jul 29 '15 at 04:29
  • Everything I have done above is very systematic. Where does it look freehanded? – robjohn Jul 29 '15 at 04:32
  • Taking the linear combinations to arrive at (7). Let m=5 and n=7, the moduli. I am writing a program for this. In other words once I finish doing egcd(5,7) =(g,a,b) = (1,3,-2), this means making those four congruences: ma mod m, ma mod n, nb mod m, and nb mod n. Correct so far? – user2597879 Jul 29 '15 at 04:36
  • By systematic I mean something that will apply to the general case and not just this one (the example was meant to help make the generalization easier to understand), because for example you can't always multiple (5) by 1 and (6) by 2. – user2597879 Jul 29 '15 at 04:39
  • Note that we are simply combining the vectors $\begin{bmatrix}1\0\end{bmatrix}$ from $(5)$ and $\begin{bmatrix}0\1\end{bmatrix}$ from $(6)$ to get the vector $\begin{bmatrix}1\2\end{bmatrix}$ in $(7)$ that we got from $(1)$ and $(2)$. I have highlighted these vectors in blue. – robjohn Jul 29 '15 at 04:41
  • @user2597879: does this make sense, or is there something still confusing? Should I work another example? – robjohn Jul 29 '15 at 05:23
  • @robjohn Still confusing but I don't think this particular question is necessarily best for what I am trying to do – user2597879 Jul 29 '15 at 13:19
  • We get $(1)$ and $(2)$ from considering the residues modulo the GCD of the moduli; that is, $x\equiv2\pmod{3}$. We use the Extended Euclidean Algorithm in $(3)$ to get $(4)$. We use $(4)$ to get a number which is $\begin{bmatrix}1\0\end{bmatrix}$ mod $\begin{bmatrix}5\7\end{bmatrix}$ in $(5)$ and a number which is $\begin{bmatrix}0\1\end{bmatrix}$ mod $\begin{bmatrix}5\7\end{bmatrix}$ in $(6)$ so that we can easily compute a number which is $\begin{bmatrix}1\2\end{bmatrix}$ mod $\begin{bmatrix}5\7\end{bmatrix}$ in $(7)$ to solve the equations in $(1)$ and $(2)$. – robjohn Jul 29 '15 at 13:28
  • @user2597879 I posted an answer using another method which you might find simpler. There are various ways to solve such systems. – Bill Dubuque Jun 20 '19 at 18:01
0

It's a bit simpler if we proceed as follows

$\bmod 15\!:\,\ 5\equiv x\equiv 8\!+\!21\,\color{#c00}k\equiv 8\!+\!6k\ \ \overbrace{\!\!\iff 6k\equiv -3\iff\bmod\color{#c00} 5\!:\,\ 2k\equiv -1}^{\Large\! 15j\,+\,6k\ =\ -3\ \ \ \ \overset{\LARGE (\ \ )/3}\iff\ \ \ \ 5j\,+\,2k\ =\ -1 }\equiv 4\iff \color{#c00}{k\equiv 2}$

So we conclude $\, x = 8\!+\!21(\color{#c00}{2\!+\!5}n) = 50\! +\! 105n$

Remark $ $ See here for the general method of transforming the Bezout solution into a CRT solution.

Bill Dubuque
  • 272,048