2

This question comes from D. E. Knuth's book "The Art of Computer Programming". I have spent a little while now trying to figure this out but haven't come up with a solution. Thanks to anyone who can help!

Edit: I suppose I should clarifiy that I know the answer is 8 but I was wondering if there is a better way to go about this problem than just checking every number from 0 to 15. For instance if I ever got a similar problem where the numbers were much larger how would I solve it?

Phillip
  • 33

4 Answers4

2

$$x \equiv 2 \pmod 3 \Rightarrow 5x \equiv 10 \pmod {15}$$ $$x \equiv 3 \pmod 5 \Rightarrow 3x \equiv 9 \pmod {15}$$

Subtracting $$2x \equiv 1 \pmod {15} \Rightarrow x \equiv 8 \pmod {15}$$

Ant
  • 21,522
1

Since you ask here is the way to attack the general question. Let $a_{1},a_{2},\ldots, a_{n}$ be pairwise relatively prime positive integers. Suppose that we want to find an integer $m \equiv b_{i}$ (mod $a_{i}$) for each $i.$ This is easy if $n= 1.$ If $n > 1,$ let $\pi_{i} = \prod_{j \neq i} a_{j}$ for each $i.$ Let's look at $\sum_{i=1}^{n}b_{i} \pi_{i}.$ If we take any $a_{k},$ we see that $a_{k}$ divides $\pi_{i}$ for each $i \neq k.$ Hence we see that $\sum_{i=1}^{n}b_{i} \pi_{i} \equiv b_{k} \pi_{k}$ (mod $a_{k}$). This is not quite what we want, but we can fix it: notice that $a_{k}$ and $\pi_{k}$ are relatively prime, so we can write $c_{k} a_{k} + d_{k} \pi_{k} = 1 $ for integers $c_{k}$ and $d_{k}.$ Hence $d_{k} \pi_{k} \equiv 1$ (mod $a_{k}$). Hence the integer we really want is $\sum_{i=1}^{n} b_{i}d_{i} \pi_{i}.$ Let's check this out in the case asked about. Here $a_{1} = 3,a_{2} = 5.$ We see that $\pi_{1} = 5$ and $\pi_{2} = 3.$ Now $1 = 2 \times a_{1}- 1 \times \pi_{1}$ and $1 = 2 \times \pi_{2} - 1 \times a_{2}.$ Hence $d_{1} = -1$ and $d_{2} = 2.$ The number we want then is $b_{1}d_{1}\pi_{1} + b_{2}d_{2}\pi_{2}.$ Here $b_{1} = 2$ and $b_{2} = 3,$ so the integer we want is $-10 +18 = 8,$ as found by various other methods.

0

a simple guess shows you that $8$ satisfies your conditions. now to show that this is in fact the only possibility you can substract $8$ from your original number and observe that it is now divisible by both $3$ and $5$, hence also by $15$ (since they're co-prime)

mm-aops
  • 4,303
  • 21
  • 32
0

Hint $\,\ x \equiv 3\pmod 5\iff x\equiv 3,\color{#0a0}8,13\pmod{15}\ $ but only $\ \color{#0a0}8\equiv 2\pmod 3$

The above method will generally be the quickest when one of the moduli $\,m\,$ is very small, since it involves checking only $\,m\,$ candidate solutions. Another optimization also applies to this system: the Bezout identity for the moduli $\,m,n = 5,3\,$ is obvious since $\,5\ {\rm mod}\ 3\, =\, 5-2\cdot 3\, =\, -1,\, $ so $\ \ \color{#c00}{2\cdot 3}+\color{#c00}{(-1)5} = 1,\,$ and we can "read off" the solution from this Bezout identity as follows:

$\ \qquad\color{#c00}{j m} + \color{#c00}{k n} = 1\ \ \Rightarrow\ \begin{eqnarray}&&x\equiv a\!\!\!\pmod m\\ &&x\equiv b\!\!\!\pmod n\end{eqnarray}$ $\!\iff\!$ $\begin{eqnarray} x&\equiv&\ a\,\color{#c00}{kn}\, +\, b\,\color{#c00}{jm}&&({\rm mod}\ {mn})\\ &\equiv& a+(b\!-\!a)\color{#c00}{jm}&&({\rm mod}\ mn)\end{eqnarray}$

$\ \ \color{#c00}{2\cdot 3} + \color{#c00}{(-1)5} = 1\ \ \Rightarrow\ \begin{eqnarray}&&x\equiv a\!\!\!\pmod 3\\ &&x\equiv b\!\!\!\pmod 5\end{eqnarray}$ $\!\iff\!$ $\begin{eqnarray} x&\equiv&\ a\,\color{#c00}{(-5)} + b\,\color{#c00}{(6)}&&({\rm mod}\ {mn})\\ &\equiv&\ a+(b\!-\!a)\,\color{#c00}{(6)}&&({\rm mod}\ mn)\end{eqnarray}$

Hence for the special case $\ a,b\, =\,2,3\ $ we obtain $\ x\equiv 2+(3\!-\!2)\color{#c00}{(6)} = 8.\,$ With only a little practice, one can do the above in $10-30$ seconds of mental arithmetic in small cases.

The same optimization works generally when one of the moduli is $\pm1$ modulo the other, since that immediately yields the Bezout identity for their gcd $= 1.\,$ This essentially optimizes the extended Euclidean algorithm when it terminates in a single step.

Bill Dubuque
  • 282,220