Number Theory – Verifying Relatively Prime Property

computer sciencenumber theoryprime numberspuzzle

I am reading a computer science puzzles book.

And I get the following question –

"You have a five quart jug, a three quart jug and unlimited supply of water. How would you come up with exactly four quarts of water ?"

The question is pretty simple and I figured the sequence of "water transfers" to solve it.

But there was an interesting side-note that I did not know.

The side-note said that as long as the two jugs size relatively prime, it is possible to find a pour sequence for any value between one and the sum of the jug sizes.

It is true for 5 and 3.

But I want to know if it is really generally true or not ? If it is, it is really cool! What is the theorem called ? Who discovered it ?

Best Answer

Let two wine glasses A and B have capacities $a$ and $b$ ounces, where $a$ and $b$ are relatively prime positive integers, and $a\lt b$. Suppose we also have a large open barrel full of wine. We will show that for any integer $w$, where $0\le w\le b$, we can measure out $w$ ounces of wine. (This means that we can measure out any integer number $y\le a+b$ of wine ounces, as long as we don't mind the stuff being possibly in two glasses. I don't.)

It is enough to show that for any $r$, with $0\le r\lt a$, we can measure out $r$ ounces. For $w=qa+r$ for some $r$ between $0$ and $a-1$. So if we can measure out $r$ ounces, we can put this into B, and then get to $w$ by using glass A $q$ times.

The construction goes as follows. Suppose that at a certain stage we have $x_k$ ounces of wine in cup A. Fill cup B from the barrel, top up cup A from cup B, pour the contents of A into the barrel, or better, drink it. Keep on filling A from cup B, dumping the contents of A into the barrel when A gets full. After a while, the amount left in B will be insufficient to fill A, but pour it in anyway. Call the amount that is now in A by the name $x_{k+1}$. Then $$b=(a-x_k)+an+x_{k+1}$$ for some integer $n$. It follows that $$x_{k+1}\equiv x_k +b\pmod{a}.$$ Start with A empty, that is, with $x_0=0$. Then $x_1\equiv b\pmod{a}$, $x_2\equiv 2b\pmod{a}$, and so on.

But since $a$ and $b$ are relatively prime, the numbers $0,b,2b.3b,\dots, (a-1)b$ form a complete residue system modulo $a$. Thus the sequence $x_0,x_1,x_2,\dots,x_{a-1}$ runs, in some order, through all the numbers from $0$ to $a-1$. This completes the proof.