# Linear diophantine equation $100x - 23y = -19$

I need help with this equation: $$100x - 23y = -19.$$ When I plug this into Wolfram|Alpha, one of the integer solutions is $x = 23n + 12$ where $n$ is a subset of all the integers, but I can't seem to figure out how they got to that answer.

• If you type in ExtendedGCD[100, 23], Wolfram Alpha will respond $\{1, \{3, -13\}\}$ Which means that the GCD of $100$ and $23$ is $1$ and that $3(100)-13(23) = 1$ Multiply both sides by -19 and you get a particular solution. Don't abuse this function! You still need to learn how to do it correctly. May 15, 2015 at 21:29

Below is an implementation of the Extended Euclidean Algorithm.

Using the Euclid-Wallis algorithm (described below) $$\begin{array}{r} &&4&2&1&7\\\hline 1&0&1&-2&3&-23\\ 0&1&-4&9&-13&100\\ 100&23&8&7&1&0\\ &&&&{\uparrow}&{\star} \end{array}$$ lookng below the horizotal line, the top row times $$100$$ plus the middle row times $$23$$ equals the bottom row. Therefore, the arrow column says that $$3\cdot100-13\cdot23=1$$ Furthermore, the top and middle numbers in each column are relatively prime. Therefore, the star column gives the smallest combination of $$100$$ and $$23$$ that equals $$0$$. Add arbitrary multiples of the star column to a particular multiple of the arrow column to get all solutions for a particular problem. Since we want a result of $$-19$$, add arbitrary multiples of the star column to $$-19$$ times the arrow column: \begin{align} -19&=(-19\cdot3-23k)100+(-19\cdot-13+100k)23\\ &=(-57-23k)100+(247+100k)23 \end{align} This gives all the integer solutions. Thus, $$x=-57-23k=12-23(k+3)=12+23n$$ where $$n=-k-3$$.

Euclid-Wallis Algorithm

This algorithm computes $$\gcd(m,n)$$, solves the Diophantine equation $$mx + ny = \gcd(m,n)$$, and yields the continued fraction for $$m/n$$.

Start with the two columns $$\begin{array}{r} 1&0\\ 0&1\\ m&n\\ \end{array}\tag{1}$$ Above each successive column write down the floor of the quotient of the base of the previous column divided into the base of the column before that, then compute the next column by subtracting that number times the previous column from the column before that.

Let us work an example; $$m = 17, n = 23$$: $$\newcommand{\nextq}{\leftarrow\lfloor\color{##00A000}{#1}/\color{##0000FF}{#2}\rfloor} \newcommand{\euclid}{{\leftarrow}\color{##00A000}{#1}-\color{orange}{#3}\cdot\color{##0000FF}{#2}} \begin{array}{rcl} \begin{array}{l} \color{#C00000}{\text{Above each successive column}}\\ \text{write down the floor of the}\\ \text{quotient of }\color{#0000FF}{\text{the base of the}}\\ \color{#0000FF}{\text{previous column}}\text{ divided into }\color{#00A000}{\text{the}}\\ \color{#00A000}{\text{base of the column before that}} \end{array} && \begin{array}{l} \text{Compute }\color{#C00000}{\text{the next column}}\text{ by}\\ \text{subtracting }\color{orange}{\text{that number}}\text{ times}\\ \color{#0000FF}{\text{the previous column}}\text{ from }\color{#00A000}{\text{the}}\\ \color{#00A000}{\text{column before that}}\\ \text{} \end{array}\\ \begin{array}{rrrl} & & \color{#C00000}{0} & \nextq{17}{23}\\ \hline 1 & 0 \\ 0 & 1 \\ \color{#00A000}{17} & \color{#0000FF}{23} \end{array} &\rightarrow& \begin{array}{rrrl} & & \color{orange}{0}\\ \hline \color{#00A000}{1} & \color{#0000FF}{0} & \color{#C00000}{1} & \euclid{1}{0}{0} \\ \color{#00A000}{0} & \color{#0000FF}{1} & \color{#C00000}{0} & \euclid{0}{1}{0} \\ \color{#00A000}{17} & \color{#0000FF}{23} & \color{#C00000}{17} & \euclid{17}{23}{0} \end{array}\\ &\swarrow&\\ \begin{array}{rrrrl} & & 0 & \color{#C00000}{1} & \nextq{23}{17} \\ \hline 1 & 0 & 1 \\ 0 & 1 & 0 \\ 17 & \color{#00A000}{23} & \color{#0000FF}{17} \end{array} &\rightarrow& \begin{array}{rrrrl} & & 0 & \color{orange}{1} \\ \hline 1 & \color{#00A000}{0} & \color{#0000FF}{1} & \color{#C00000}{-1} & \euclid{0}{1}{1} \\ 0 & \color{#00A000}{1} & \color{#0000FF}{0} & \color{#C00000}{1} & \euclid{1}{0}{1} \\ 17 & \color{#00A000}{23} & \color{#0000FF}{17} & \color{#C00000}{6} & \euclid{23}{17}{1} \end{array}\\ &\swarrow&\\ \begin{array}{rrrrrl} & & 0 & 1 & \color{#C00000}{2} &\nextq{17}{6} \\ \hline 1 & 0 & 1 & -1 \\ 0 & 1 & 0 & 1 \\ 17 & 23 & \color{#00A000}{17} & \color{#0000FF}{6} \end{array} &\rightarrow& \begin{array}{rrrrrl} & & 0 & 1 & \color{orange}{2} \\ \hline 1 & 0 & \color{#00A000}{1} & \color{#0000FF}{-1} & \color{#C00000}{3} & \euclid{1}{-1}{2} \\ 0 & 1 & \color{#00A000}{0} & \color{#0000FF}{1} & \color{#C00000}{-2} & \euclid{0}{1}{2} \\ 17 & 23 & \color{#00A000}{17} & \color{#0000FF}{6} & \color{#C00000}{5} & \euclid{17}{6}{2} \end{array} \end{array}\\ \vdots$$ $$\begin{array}{rrrrrrr} &&\color{orange}{0}&\color{orange}{1}&\color{orange}{2}&\color{orange}{1}&\color{orange}{5}\\ \hline \color{#00A000}{1}&\color{#00A000}{0}& 1&-1& 3&\color{#C00000}{-4}&\color{#0000FF}{23}\\ \color{#00A000}{0}& \color{#00A000}{1}& 0&1&-2&\color{#C00000}{3}&\color{#0000FF}{-17}\\ \color{#00A000}{17}&\color{#00A000}{23}&17& 6& 5& \color{#C00000}{1}&\color{#0000FF}{0} \end{array}\tag{2}$$ The red column (preceding the blue one with $$0$$ at its base) has $$\gcd(m,n)$$ at its base. Also, $$m$$ times the top row plus $$n$$ times the middle row equals the bottom row. Thus, the red column (with $$\gcd(m,n)$$ at its base) has the coefficients for $$mx + ny = \gcd(m,n)$$ in the top two rows.

Also, the blue column (with $$0$$ at its base) has the smallest non-zero solution to $$mx + ny = 0$$. Multiples of this solution can be added to a particular solution of $$mx + ny = k$$ to get all solutions.

The orange quotients above the columns yield the continued fraction for $$m/n$$.

Thus, $$\gcd(\color{#00A000}{17},\color{#00A000}{23}) = \color{#C00000}{1}\\ (\color{#C00000}{-4}\color{#0000FF}{+23}k)\color{#00A000}{17} + (\color{#C00000}{3}\color{#0000FF}{-17}k)\color{#00A000}{23} = \color{#C00000}{1}$$ and the continued fraction for $$\color{#00A000}{17}/\color{#00A000}{23}$$ is $$[\color{orange}{0},\color{orange}{1},\color{orange}{2},\color{orange}{1},\color{orange}{5}]$$.

Euclidean Algorithm (plus bookkeeping)

The algorithm above is simply the Euclidean algorithm in the top and bottom rows, with some bookkeeping in the two middle rows. The quotients are in the top row and the remainders (which, as dictated by the Euclidean Algorithm, later become divisors and then dividends) are in the bottom row.

In $$(2)$$, the first dividend is $$17$$ and the first divisor is $$23$$. The first quotient is $$0$$ and the remainder is $$17$$. In the next pass, the dividend is $$23$$ (which was the previous divisor) and the divisor is $$17$$ (which was the previous remainder). The second quotient is $$1$$ and the remainder is $$6$$. In the third pass the dividend is $$17$$ and the divisor is $$6$$, which yields a quotient of $$2$$ and a remainder of $$5$$. This proceeds until a remainder of $$0$$ appears. The algorithm has to stop here since the next divisor would be $$0$$.

The bookkeeping in the middle two rows mimics the computation performed in the bottom row. That is, below the line, the third column is first column minus $$0$$ times the second column. The fourth column is the second column minus $$1$$ times the third column. The fifth column is the third column minus two times the fourth column. This bookkeeping assures that the first row below the line times $$17$$ plus the second row times $$23$$ equals the bottom row. This allows us to back out the Euclidean algorithm at the same time we are performing it.

• Rob, this is an awesome reference. Thank you.
– Pedro
May 17, 2013 at 17:45
• See here for a natural viewpoint as elimination by row-reduction. This is a special case of general methods (Hermite / Smith) for triangularizing or diagonalizing matrices to normal form over Euclidean domains. Mar 4, 2015 at 15:02

$100x -23y = -19$ if and only if $23y = 100x+19$, if and only if $100x+19$ is divisible by $23$. Using modular arithmetic, you have \begin{align*} 100x + 19\equiv 0\pmod{23}&\Longleftrightarrow 100x\equiv -19\pmod{23}\\ &\Longleftrightarrow 8x \equiv 4\pmod{23}\\ &\Longleftrightarrow 2x\equiv 1\pmod{23}\\ &\Longleftrightarrow x\equiv 12\pmod{23}. \end{align*} so $x=12+23n$ for some integer $n$.

• Wow, thanks for the fast response! I followed you all the way up until '100x = -19 (mod 23)' went to '8x = 4 (mod 23)'. Can you explain that a little more?
– Mike
Sep 27, 2011 at 18:11
• $100=4\cdot 23+8$ and $-19=4+(-1)\cdot 23$ Sep 27, 2011 at 18:18

$$\!\overbrace{\bmod 23\!:\ 100x\equiv -19}^{\textstyle\ \ \ \ {-}23y+100x = -19}\!\iff\! x \equiv \dfrac{\!-19}{100} \equiv \dfrac{4}{100} \equiv \dfrac{1}{25} \equiv\dfrac{24}{2}\equiv 12,\$$ i.e. $$\ x = 12 + 23 n$$

Just as above, using modular fraction arithmetic often greatly simplifies solving linear congruences, e.g. see here, and here and here for circa $$20$$ motley worked examples via a handful of methods (and see the sidebar "Linked" questions lists there for many more).

• Beware  Modular fraction arithmetic is well-defined only for fractions with denominator coprime to the modulus. See here for further discussion. Mar 4, 2015 at 14:53

If you take the equation mod $23$, you find $8x \equiv 4 \pmod{23}$ and by inspection, this is satisfied by $x \equiv 12 \pmod{23}$. To find this, you use the Extended Euclidean algorithm

I find this version of the Euclid-Wallis Algoritmn a bit more user friendly. robjohn did a great job of explaining how it works so I offer this with no further explaination. Please note that I put the multiplier next to the row that gets multiplied by it. Also note that I multiply and then add, so my multipliers have a different sign than his. It's really the same thing, I just think this way is a bit more transparent. I also omitted the $0"$ row. I don't think it's worth the effort used to calculate it.

                   interpretation
|100   1    0   100 =  1(100) +  0(23)
-4 | 23   0    1    23 =  0(100) +  1(23)
-3 |  8   1   -4     8 =  1(100) -  4(23)
| -1  -3   13    -1 = -3(100) + 13(23)
|  1   3  -13     1 =  3(100) - 13(23)


and we conclude that 3(100)-13(23) = 1

Hence a solution to $100x - 23y = 1$ is $(x,y)=(3, -13)$

Myltiply through by $-19$ and you get

$$100(-57) + 23(247) = -19$$

Not pretty, but it's a start.

To find a general solution to $100x + 23y = -19$, you need to solve

$100x + 23y = 100(-57) + 23(247)$
$100(x+57) = 23(247-y)$

Because $100$ and $23$ are relatively prime, we must have

$x + 57 = 23k$ and $247-y = 100k$ for any $k \in \mathbb Z$

so $x = 23k-57$ and $y=247 - 100k$

If you think that $-57$ and $247$ are too ugly, you can mess with the equation some. I would try substituting $k = s + 2$ to change the $247$ to a $47$.

$x = 23(s+2)-57$ and $y=247 - 100(s+2)$

which simplifies to

$x = 23s-11$ and $y=47 - 100s$

The continued fraction solution goes as follows:

Expand 100/23 into a continued fraction (I'm essentially using the GCD algorithm):

\begin{align*} \frac{100}{23} & = 4 + \frac{8}{23}\\ & = 4 + \frac{1}{\frac{23}{8}}\\ & = 4 + \cfrac{1}{2 + \cfrac{7}{8}}\\ & = 4 + \cfrac{1}{2 + \cfrac{1}{\frac{8}{7}}}\\ & = 4 + \cfrac{1}{2 + \cfrac{1}{1 + \cfrac{1}{7}}} \end{align*}

Now that all the numerators are $1$, you're done getting the continued fraction, often written as the list of partial quotients like this:

$100/23 = [4,2,1,7]$

You can of course check: $4 + 1/(2 + 1/(1 + 1/7)) = 100/23$, the last equation above. You can change the $7$ at the end to $6 + 1/1$ to get an odd number of partial quotients but you don't have to.

Now if you look at $[4,2,1]$, which is the next to last convergent (the last being $[4,2,1,7]=100/23$) you get

$$4 + \frac{1}{2 + \frac{1}{1}} = \frac{13}{3}$$

The difference of cross product of the numerators and denominators of successive convergents is always $+1$ or $-1$, i.e:

$$100 \cdot 3 - 13 \cdot 23 = 1$$

If we multiply though by $-19$ we get:

$$100 \cdot (3 \cdot -19) - 23 \cdot (13 \cdot -19) = -19$$

so a particular solution $x_0,y_0$ is \begin{align*} x_0 & = 3 \cdot -19 = -57\\ y_0 & = 13 \cdot -19 = -247 \end{align*} Since for all integer $t$

$$100 \cdot 23t - 23 \cdot 100t = 0$$

we can add that equation to

$$100 \cdot -57 - 23 \cdot -247 = -19$$

and get

$$100(23t-57) - 23(100t-247) = -19$$

so the general solution is \begin{align*} x & = 23t - 57\\ y & = 100t - 247 \end{align*}

To get the exact Wolfram answer change variables to $n = t-3$

$$x = 23(n+3)-57 = 23n+69-57 = 23n + 12$$

• Please see this tutorial on how to typeset mathematics on this site. Sep 28, 2015 at 18:40
• You made a typographical error in your final line. $23n + 69 - 57 = 23n \color{red}{+} 12$. Sep 28, 2015 at 18:41
• Thanks for the note -- hopefully things are ok now Sep 28, 2015 at 18:44
• Thanks for the formatting help too. I think you and I tried to add formatting at the same time, but it seems to have ended up ok. Sep 28, 2015 at 18:53

[For the following paragraphs, please refer to the figure at the end of the last paragraph (the figure is also available in PDF).]

The manipulations performed from steps (0) to (16) were designed to create the linear system of equations (0a), (5a), (11a) and (16a). The manipulations end when the absolute value of a coefficient of the latest equation added is 1 (see (16a)).

Equation (0a) is given. It is possible to infer equations (5a), (11a) and (16a) from (5), (11) and (16) respectively without performing manipulations (0) to (16) directly. In every case, select the smallest absolute value coefficient, generate the next equation by replacing every coefficient with the remainder of the coefficient divided by the selected coefficient (smallest absolute value coefficient) – do the same with the right-hand constant – and add the new variable whose coefficient is the smallest absolute value coefficient. If the new equation has a greatest common divisor greater than one, divide the equation by the greatest common divisor. Stop when the absolute value of a coefficient of the latest equation added is 1.

Then proceed to solve the linear system of equations. if you put $x=23n+12$ into equation $100x-23y=-19$ you will have $y$ as a linear function of $n$ then you can put various n and find (x,y) such that x,y are solution of equation $$x=23n+12 \\100(23n+12)-23y=-19\\100(23n)+1200-23y=-19\\23y=100(23n)+1219$$ divide by $23$ $$y=100n+53$$ so $$n\in \mathbb{Z}\\\left\{\begin{matrix} x=23n+12\\ y=100n+53 \end{matrix}\right.$$

For equation $$100x - 23y = -19$$, I use scaling method.
GCD(100,79) calculation numbers, in reversed order:

1
7 → -floor(1/7*8) = -1 = inverse of 7 (mod 8)
8 → -floor(-1/8*23) = 3 = inverse of 8 (mod 23)
23 → -floor(3/23*100) = -13 = inverse of 23 (mod 100)
100

Mod 23 : $$100x ≡ -19 → x≡ (-19)(100^{-1})≡ (4)(8^{-1})≡ (4)(3)≡ 12$$

Mod 100 : $$-23y≡ -19 → y≡ (19)(23^{-1})≡ (19)(-13)≡ -247≡ 53$$

Another way is by guessing a multiplier. This work best for prime modulo.
For composite modulo, multiplier had to confirm co-prime to the modulo.

$$100x ≡ -19 \text{ (mod 23)}$$

$${100 \over 23} ≈ 4.3478 ≈ {13 \over 3}$$. Multiply both side by 3, we get:

$$300x ≡ x ≡ -19\times 3 ≡ 12 \text{ (mod 23)}$$