Video 2, chapter 7

The Water-Jug Puzzle

How unmarked 5-liter and 3-liter jugs measure exactly 4 liters in the water pouring puzzle, why only full and empty jugs give exact measurement, and how the greatest common divisor decides which amounts are possible.

  • Video 2, chapter 7
  • Plan the moves

You have a 5-liter jug, a 3-liter jug, a tap and a drain, and neither jug has any markings. How can you end up with exactly 4 liters in the larger jug? This page solves the puzzle by working backward from the goal, records every step as a pair of amounts that can be checked by arithmetic, and shows how the greatest common divisor of the two capacities decides which amounts can be measured at all.

Key terms

Water pouring puzzle

Definition The water pouring puzzle, also called the water jug puzzle, gives two jugs without markings whose capacities are aa and bb liters, where aa and bb are positive whole numbers. Both jugs start empty, and there is an unlimited supply of water and a drain. Three kinds of actions are allowed. You may fill a jug from the supply until it is full, empty a jug completely into the drain, or pour from one jug into the other until the first jug is empty or the second is full, whichever happens first. No other way of stopping a pour is allowed. The goal is to leave a given amount of water in one of the jugs. In the classic case the jugs hold 5 and 3 liters and the goal is exactly 4 liters in the 5-liter jug. This can be done in six actions, and a search through every state the jugs can reach shows that no shorter sequence works. Which amounts can be reached for other capacities is settled by the greatest common divisor, in the last card below.

In plain words The jugs can only be trusted when they are full or empty. The solution uses the small jug twice, once to take 3 liters out of a full large jug and once, with 2 liters already inside, to take out exactly 1 liter.

Example Call the 5-liter jug the large jug and the 3-liter jug the small jug, and work backward from the goal. A full large jug drops to exactly 4 liters if it is poured into a small jug that already holds 2 liters, because that jug has room for only 1 more. So the task becomes putting 2 liters into the small jug, and 2 liters is what stays behind when a full large jug fills the empty small jug. Running these ideas forward gives the solution.

StepActionLarge jugSmall jug
1fill the large jug50
2pour large into small23
3empty the small jug20
4pour large into small02
5fill the large jug52
6pour large into small43

After step 6 the large jug holds exactly 4 liters, and every pour stopped because a jug became full or empty.

Exact measurement

Definition In the water pouring puzzle an amount is measured exactly when the allowed actions alone produce it, without judging a water level by eye. Every allowed action stops at a boundary, a jug that is full or empty, so its result follows from the capacities and the current amounts. Record the situation as a state (x,y)(x, y), where xx and yy are the amounts in the first and the second jug. Filling a jug sets its amount to its capacity, and emptying it sets its amount to 00. Pouring the first jug into the second, whose capacity is bb, moves

m=min⁡(x, b−y)m = \min(x,\ b - y)

liters, the smaller of the amount in the first jug and the room left in the second, and leads to the state (x−m, y+m)(x - m,\ y + m). A pour the other way works the same with the roles of the jugs exchanged. After every action at least one jug is full or empty.

In plain words A full jug and an empty jug are the only readings you can trust, so every action has to end at one of them. Writing each state as a pair of numbers lets you check every step with arithmetic instead of with your eyes.

Example Filling the large jug and pouring out what looks like 1 liter is not allowed, because nothing signals the moment when exactly 1 liter has left. The six steps of the solution, with the large jug first, run through the states

(0,0)→(5,0)→(2,3)→(2,0)→(0,2)→(5,2)→(4,3).\begin{gathered} (0, 0) \to (5, 0) \to (2, 3) \\[0.4em] \to (2, 0) \to (0, 2) \\[0.4em] \to (5, 2) \to (4, 3). \end{gathered}

The three pours move min⁡(5,3−0)=3\min(5, 3 - 0) = 3 liters, then min⁡(2,3−0)=2\min(2, 3 - 0) = 2 liters, then min⁡(5,3−2)=1\min(5, 3 - 2) = 1 liter. In every state one jug is full or empty, and the total amount of water changes only when a jug is filled or emptied.

Multiples

Definition For whole numbers n≥0n \ge 0 and d≥1d \ge 1, the number nn is a multiple of dd when n=kdn = kd for some whole number k≥0k \ge 0. Then dd is called a divisor of nn, and we say dd divides nn. The sum of two multiples of dd is a multiple of dd, and so is the difference of a larger multiple and a smaller one. In the water pouring puzzle this gives an invariant, a property that no allowed action can break. If dd divides both capacities, then every amount that ever appears in either jug is a multiple of dd. Filling gives a capacity, emptying gives 00, and a pour moves min⁡(x,b−y)\min(x, b - y) liters, which is a multiple of dd whenever xx, yy and bb are.

In plain words If both jugs hold an even number of liters, every action keeps every amount even. An odd amount can then never appear, however clever the sequence of actions.

Example Take jugs of 6 and 4 liters, both multiples of 2. Starting from (0,0)(0, 0), the states that can be reached are exactly these ten, with the 6-liter jug first.

(0,0), (6,0), (0,4),(6,4), (2,4), (2,0),(0,2), (6,2), (4,0),(4,4)\begin{gathered} (0, 0), \ (6, 0), \ (0, 4), \\[0.4em] (6, 4), \ (2, 4), \ (2, 0), \\[0.4em] (0, 2), \ (6, 2), \ (4, 0), \\[0.4em] (4, 4) \end{gathered}

Every amount is even, so exactly 3 liters can never be measured in either jug, and neither can 1 or 5 liters. The capacities 5 and 3 have no common divisor larger than 1, so nothing of this kind blocks the classic puzzle.

Greatest common divisor

Definition The greatest common divisor of positive whole numbers aa and bb, written gcd⁡(a,b)\gcd(a, b), is the largest whole number that divides both of them. It decides the water pouring puzzle under the rules above, with positive whole-number capacities aa and bb, two jugs that start empty, an unlimited supply and drain, and pours that stop only when the jug being poured from becomes empty or the jug being poured into becomes full. Let g=gcd⁡(a,b)g = \gcd(a, b). Then the jug of capacity aa can be left holding exactly tt liters if and only if tt is a multiple of gg with 0≤t≤a0 \le t \le a, and the same holds for the jug of capacity bb, with bb in place of aa. The two jugs together can hold any multiple of gg from 00 to a+ba + b, and no other amount. The theorem depends on these rules. Variants with different rules, for example jugs that start full with no supply or drain, need their own analysis.

In plain words The greatest common divisor is the smallest positive amount the two jugs can measure, and every amount they can measure is a whole number of these units. Every such amount that fits in a jug can be left in that jug.

Example For the classic puzzle gcd⁡(5,3)=1\gcd(5, 3) = 1, so the large jug can be left with any whole number of liters from 0 to 5, and the small jug with any from 0 to 3. For the 6-liter and 4-liter jugs of the previous card gcd⁡(6,4)=2\gcd(6, 4) = 2, which explains the even amounts. To see why every multiple of gg can be reached, repeat one cycle with the 5-liter and 3-liter jugs. Fill the small jug, pour it into the large jug, and whenever the large jug becomes full, empty it and keep pouring. Each time the small jug runs dry, the large jug holds 3, 1, 4 and 2 liters, the remainders of 3, 6, 9 and 12 on division by 5, and after that it is full. For any capacities, the same cycle leaves in the jug of capacity aa the remainders of bb, 2b2b, 3b3b and so on on division by aa, and these remainders run through every multiple of gg below aa. The cycle reaches 4 liters in eight actions, two more than the plan worked backward. The capacity limit matters as well. A greatest common divisor of 1 does not put 7 liters into a 5-liter jug, although the two jugs together hold 7 liters in the final state (4,3)(4, 3) of the solution.

Check yourself

  1. With the same 5-liter and 3-liter jugs, leave exactly 1 liter in one of the jugs.
Show the answer

Fill the small jug and pour it into the empty large jug, which then holds 3 liters. Fill the small jug again and pour it into the large jug until the large jug is full. The large jug had room for only 2 more liters, so exactly 1 liter stays in the small jug. With the large jug first, the states are

(0,3)→(3,0)→(3,3)→(5,1).\begin{gathered} (0, 3) \to (3, 0) \\[0.4em] \to (3, 3) \to (5, 1). \end{gathered}

Four actions are enough, and a search of all reachable states shows that no shorter sequence leaves 1 liter in either jug. This agrees with the theorem, since gcd⁡(5,3)=1\gcd(5, 3) = 1 and 1 liter fits in both jugs.

  1. Now the jugs hold 9 and 6 liters. Which amounts can be left in the 9-liter jug? Can exactly 4 liters be measured in either jug?
Show the answer

The greatest common divisor is gcd⁡(9,6)=3\gcd(9, 6) = 3, so the 9-liter jug can be left with exactly 0, 3, 6 or 9 liters and nothing else. Each of these is easy to reach. Filling the 9-liter jug and pouring it into the empty 6-liter jug leaves 3 liters, filling the 6-liter jug and pouring it into the empty 9-liter jug gives 6 liters, and filling the 9-liter jug gives 9. Exactly 4 liters is impossible in either jug, because both capacities are multiples of 3 and every allowed action keeps every amount a multiple of 3. The jugs are large enough to hold 4 liters, but capacity alone is not enough. The target has to be a multiple of the greatest common divisor too.

  1. Extend the pattern of the classic solution. With a 7-liter jug and a 4-liter jug, leave exactly 6 liters in the 7-liter jug.
Show the answer

Work backward as before. A full 7-liter jug drops to 6 liters if it is poured into a 4-liter jug that already holds 3 liters, and 3 liters is what stays behind when a full 7-liter jug fills the empty 4-liter jug. So fill the 7-liter jug, pour it into the 4-liter jug, empty the 4-liter jug, pour the 3 liters across, fill the 7-liter jug again and pour until the 4-liter jug is full. With the 7-liter jug first, the states are

(7,0)→(3,4)→(3,0)→(0,3)→(7,3)→(6,4).\begin{gathered} (7, 0) \to (3, 4) \to (3, 0) \\[0.4em] \to (0, 3) \to (7, 3) \to (6, 4). \end{gathered}

The last pour moves min⁡(7,4−3)=1\min(7, 4 - 3) = 1 liter, so exactly 6 liters remain. The classic puzzle works the same way, with 5−3=25 - 3 = 2 liters, then room for 3−2=13 - 2 = 1 liter, then 5−1=45 - 1 = 4 liters.