LibraryQuant brainteasers

Three and Five Litre Jug Puzzle Solution

Quant brainteaser · intro · free, full solution

The question

You have a three litre jug and a five litre jug, neither with any markings on it, and an unlimited supply of water from a tap. The only moves available are: fill a jug completely, empty a jug completely, or pour from one jug into the other until either the source jug is empty or the destination jug is full.

Get exactly four litres of water into one of the two jugs.

Hint

Track the two amounts as a pair after every move. A pour always stops when one jug empties or the other fills, so think about what a full small jug can leave behind in the large one.

The idea behind it

Every reachable amount is an integer combination of the two jug sizes. Two numbers with no common factor can combine, through repeated addition and subtraction, to reach every integer between them, so any target amount is reachable as long as it shares no forced common factor with the jug sizes.

Worked solution

Track the state as a pair (amount in the five litre jug, amount in the three litre jug), starting at (0, 0).

Fill the five litre jug: (5, 0). Pour from the five litre jug into the three litre jug until the three litre jug is full: this moves 3 litres across, leaving (2, 3). Empty the three litre jug: (2, 0). Pour the remaining 2 litres from the five litre jug into the three litre jug: (0, 2). Fill the five litre jug again: (5, 2). Pour from the five litre jug into the three litre jug, which only needs 1 more litre to be full: this moves 1 litre across, leaving (4, 3).

The five litre jug now holds exactly 4 litres.

Line chart of Litres in the three litre jug against Litres in the five litre jug. The six pours. 0: 0; 5: 0; 2: 3; 2: 0; 0: 2; 5: 2; 4: 3.Litres in the three litre jug0123012345Litres in the five litre jugfour litres in the five litre jugLine chart of Litres in the three litre jug against Litres in the five litre jug. The six pours. 0: 0; 5: 0; 2: 3; 2: 0; 0: 2; 5: 2; 4: 3.Litres in the three litre jug0123024Litres in the five litre jugfour litres inthe five litrejug
A pour always ends with one jug empty or the other full, so the six moves only ever land on the edges of the grid of possible states, and four litres is one of the corners that grid reaches.

Why this is not luck: every pour either fills the destination or empties the source, so at every step the amount in each jug is some combination of adding and subtracting full 3s and full 5s. Concretely, each jug's content at every stage can be written as 5a + 3b for integers a and b, positive or negative, because filling contributes a full jug, emptying removes a full jug's worth from the running total, and pouring transfers the difference. Since 5 and 3 share no common factor other than 1, a basic fact about integers (their greatest common divisor is 1) guarantees that some integer combination of 5s and 3s equals any target integer, including 4, since 4 = 5(2) + 3(-2) is one such combination among infinitely many. The jug moves above are exactly one concrete way of executing that combination, one pour at a time, while respecting the physical limits of never having a jug hold less than 0 or more than its capacity.

The same reasoning is why a 4 and a 6 litre jug can never measure any odd amount: both capacities share the common factor 2, so every reachable amount is a multiple of 2 as well, and no combination of fillings, emptyings, and pours can escape that. The measurable amounts with jugs of size p and q are exactly the multiples of the greatest common divisor of p and q, up to the larger jug's capacity, which is a special case of a fact usually met first in number theory as Bezout's identity.

The transferable idea is to recognise a physical puzzle as secretly a question about which integers a fixed pair of step sizes can reach through addition and subtraction, since that reframing turns trial and error into a fact that can be checked in one line: compute a greatest common divisor, and see whether the target is a multiple of it.

In short

Fill the five litre jug, pour into the three litre jug, empty the three litre jug, pour the remainder across, refill the five litre jug, and top off the three litre jug from it.

Learn quant finance on Thuztra Learn

Thuztra Learn teaches the maths of quant finance from zero, in short lessons, for readers who are unsure of it. Three questions in the app build your quant roadmap, free. Membership is a paid subscription. See what Thuztra Learn is and what it costs, or see the whole Core quant curriculum.

Get the app and answer the three questions:

Download on the Microsoft Store Download for Android

On Android, the app is a direct download, not a Google Play listing. Which devices, and what each one needs.

Further reading

Topics

Also in the library