Water Jugs by Diophant
Water Jugs by Diophant
Water Jugs by Diophant
On Algorithmic Approach to
the Two Jugs Problem
Yiu-Kwong Man
Copyright
c 2015 Yiu-Kwong Man. This is an open access article distributed under the
Creative Commons Attribution License, which permits unrestricted use, distribution, and
reproduction in any medium, provided the original work is properly cited.
Abstract
The two jugs problem is a classic problem in discrete mathematics,
recreational mathematics, artificial intelligence, problem solving, cogni-
tive psychology and computer programming. The methods of solutions
are often based on heuristics or search methods, which could be time
and memory consuming. In this paper, we introduce a simple algo-
rithmic approach to tackle this problem and discuss how to obtain the
optimal solution. Some illustrative examples are provided.
1 Introduction
The two jugs problem can be dated back to the Middle Ages in Europe. Ac-
cording to [12], the earliest version of the problem appeared in print was likely
due to the Italian mathematician Nicolo Tartaglia (1500-1557). Then, it devel-
ops to become a classic problem in problem-solving [1, 10, 12, 15, 16], geometry
[2], recreational mathematics [3], cognitive psychology [4, 8, 11, 13], computer
programming [5, 7], discrete mathematics [6] and artificial intelligence [14],
etc. A common version of the problem mentioned in books or journal articles
is like this:
1788 Yiu-Kwong Man
You are given a 3 liter jug and a 5 liter jug. The jugs do not have markings
to measure smaller quantities. How can you measure 4 liter of water by the
jugs?
There are a number of ways to solve this problem, including the working
backward approach [1], the billiard approach [2, 3], the digraph approach [6],
the search approach (Breadth First Search or Depth First Search) [5, 7, 14]
and the methods of heuristics [4, 8, 10, 11, 12, 13, 15, 16], etc. However, most
of them are not algorithmic in nature and the methods of solutions could be
time and memory consuming sometimes. Also, there is no guarantee that the
solutions obtained are optimal, in the sense that the problem can be solved in
minimal number of steps. In [17, 18], an arithmetic approach was proposed
by the author to solve this problem. This approach is algorithmic in nature
and we can apply it to determine the total amount of water in the jugs in
each pouring step by simple additions and subtractions only. Then, the actual
pouring sequence can be obtained easily. In this paper, we will introduce such
an approach and discuss how to obtain the optimal solution of the general two
jugs problem.
The whole paper is organized like this. In section 2, the arithmetic ap-
proach for solving the general two jugs problem is introduced, together with
descriptions of the mathematical background behind. Illustrations on how it
works are provided in section 3. The main result on determining the optimal
solution of the general two jugs problem is discussed in section 4. Then, some
concluding remarks are provided in the final section.
2 An Arithmetic Approach
The general two jugs problem can be stated as follows:
You are given a m-liter jug and a n-liter jug, where m, n, d are distinct
positive integers, 0 < m < n and d < n. The jugs do not have markings to
measure smaller quantities. How can you measure d liters of water by the jugs?
For example, the two jugs problem mentioned in the introduction can be
described by the Diophantine equation 3x + 5y = 4, which is solvable since
On algorithmic approach to the two jugs problem 1789
gcd(3, 5) divides 4. However, if we replace the jugs by a 3-litre jug and a 6-litre
jug, then the problem is unsolvable since gcd(3, 6) does not divide 4.
Depending on which jug is chosen to fill first, we have two possible ways
to solve the general two jugs problems, which are described by the algorithms
below [17,18]:
Algorithm 2.2.
Input: The integers m, n and d, where 0 < m < n and d < n.
Output: An integer sequence (called M1 ) corresponding to a feasible solution
of the two jugs problem, by filling the m-litre jug first.
Procedure:
Step 1: Initialize a dummy variable k = 0.
Step 2: If k 6= d, then repeat adding m to k and assign the result to k until
k = d or k > n.
Step 3: If k > n, then subtract n from k and assign the result to k.
Step 4: If k = d, then stop and return the sequence of the values of k obtained.
Otherwise, repeat the Steps 2-4.
The number of additions (say x1 ) and subtractions (say y1 ) involved pro-
vides a solution to the Diophantine equation mx + ny = d, namely x = x1 ,
y = −y1 . Then, the actual pouring sequence can be obtained easily by referring
to the integer sequence.
Algorithm 2.3.
Input: The integers m, n and d, where 0 < m < n and d < n.
Output: An integer sequence (called M2 ) corresponding to a feasible solution
of the two jugs problem, by filling the n-litre jug first.
Procedure:
Step 1: Initialize a dummy variable k = 0.
Step 2: If k 6= d, then add n to k and assign the result to k.
Step 3: If k > d, then repeat subtracting m from k and assign the result to k
until k = d or k < m.
Step 4: If k = d, then stop and return the sequence of the values of k obtained.
Otherwise, repeat the Steps 2-4.
Again, the number of subtractions (say x2 ) and additions (say y2 ) involved
provides a solution to the Diophantine equation mx + ny = d, say x = −x2 ,
y = y2 . Then, the actual pouring sequence can be obtained by referring to the
integer sequence.
1790 Yiu-Kwong Man
3 Examples
Let us illustrate how the algorithms work with the following examples.
Example 3.1. Suppose there are a 3-litre jug and a 5-litre jug. We want
to use them to measure 4 liter of water. Now, m = 3, n = 5, d = 4 and the
associated Diophantine equation is 3x + 5y = 4. Since gcd(3, 5) divides 4, so
this problem is solvable.
Based on the integer sequence obtained, we can describe the water pouring
sequence easily as follows, where (x, y) denotes the amounts of water inside
the 3-litre jug and the 5-litre jug respectively:
If we apply Algorithm 2.3, then we can obtain the following integer sequence
(M2 ):
0 −→ 5 −→ 2 −→ 7 −→ 4
+5 −3 +5 −3
There are 2 additions and 2 subtractions involved, so x = −2, y = 2 is a solu-
tion of the Diophantine equation 3x + 5y = 4. It means the 3-litre jug has been
completely unfilled for 2 times and the 5-litre jug has been completely filled for
2 times.
Based on the integer sequence obtained, we can describe the water pouring
sequence as follows:
Example 3.2. Suppose there are a 3-litre jug and a 7-litre jug and we want
to use them to measure 2-litre of water. Now, m = 3, n = 7, d = 2 and the
associated Diophantine equation is 3x + 7y = 2. Since gcd(3, 7) divides 5, so
this problem is solvable.
If we apply Algorithm 2.3, then we can obtain the following integer sequence
(M2 ):
0 −→ 7 −→ 4 −→ 1 −→ 8 −→ 5 −→ 2
+7 −3 −3 +7 −3 −3
Thus, the total number of pouring steps involved in M2 is 10, which is 4 steps
more than that of M1 .
1792 Yiu-Kwong Man
bxc = max{m ∈ Z | m ≤ x}
dxe = min{m ∈ Z | m ≥ x}
kn km
x = x0 − , y = y0 + , where k is an arbitrary constant. (1)
gcd(m, n) gcd(m, n)
The following theorem is the main result of this paper, which describes
how to determine the optimal solution of the associated two jugs problem if a
particular solution of the Diophantine equation mx + ny = d has been found.
On algorithmic approach to the two jugs problem 1793
k̂n k̂m
x0 = x0 − , y 0 = y0 + , (2)
gcd(m, n) gcd(m, n)
where
dze if z ≥ (dze + bzc)/2
k̂ = (3)
bzc if z < (dze + bzc)/2
and
nx0 − my0
z= · gcd(m, n) (4)
m2 + n2
Proof. By Theorem 4.5, P (x0 , y 0 ) is a solution of the Diophantine equation
mx + ny = d with the following form:
k̂n 0 k̂m
x0 = x 0 − , y = y0 + , where ` = gcd(m, n) and k̂ is an integer.
` `
Consider
k̂n 2 k̂m 2
kP k2 = (x0 − ) + (y0 + )
` `
2 2 2k̂(nx0 − my0 ) k̂ 2 (m2 + n2 )
= x0 + y 0 − +
` `2
2 2h
2 2 m +n 2 2k̂`(nx0 − my0 ) i
= x0 + y 0 + k̂ −
`2 m2 + n2
2 2h
m +n `(nx0 − my0 ) i2 (nx0 − my0 )2
= x20 + y02 + k̂ − −
`2 m2 + n2 m2 + n2
x0 = 3, y 0 = −1.
Hence, the water pouring sequence determined by P (3, −1) provides an opti-
mal solution to the associated two jugs problem, which is consistent with the
conclusion given in Example 2.
5 Concluding remarks
A simple and novel algorithmic approach for solving the general two jugs prob-
lem and an important result for determining its optimal solution is presented
in this paper. This approach is suitable for either hand calculations or imple-
mentation in common computer languages. The integer sequences for M1 or
M2 can be computed easily by simple additions and subtractions only. There
On algorithmic approach to the two jugs problem 1795
is no additional memory cost spent on searching and branching like what com-
mon search methods do when performing the pouring steps. Exploring the
possibilities of extension of this approach to tackle the more general k-jugs
(k > 2) problem and the method for determining its optimal solution will be
a meaningful research topic to pursue further.
References
[1] G. Polya, How to Solve It, Princeton University Press, New Jersey, 1945.
[4] M. E. Atword, Further explorations with a process model for water jug
problems, Mem. Cognition, 8 (1970), 182 - 192.
[12] S. Marchi, Mathematics and wine, Appl. Math. Comput., 192 (2007), 180
- 190. http://dx.doi.org/10.1016/j.amc.2007.02.150
1796 Yiu-Kwong Man
[18] Y. K. Man, A non-heuristic approach to the general two water jugs prob-
lem, J. Commun. Comput., 10 (2013), 904 - 908.