Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

Water Jugs by Diophant

Download as pdf or txt
Download as pdf or txt
You are on page 1of 10

Applied Mathematical Sciences, Vol. 9, 2015, no.

36, 1787 - 1796


HIKARI Ltd, www.m-hikari.com
http://dx.doi.org/10.12988/ams.2015.5165

On Algorithmic Approach to
the Two Jugs Problem
Yiu-Kwong Man

Department of Mathematics and Information Technology


The Hong Kong Institute of Education
10 Lo Ping Road, Tai Po, New Territories, Hong Kong

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.

Keywords: Two Jugs Problem, Diophantine Equation, Problem Solving,


Algorithm

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?

This problem can be modelled by the Diophantine equation mx + ny = d,


whose solvability is determined by the following well-known theorem in number
theory [9].

Theorem 2.1. Let m, n, d be integers. The Diophantine equation mx +


ny = d is solvable if and only if gcd(m, n) divides d.

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.

Unless stated otherwise, let us assume mx + ny = d is solvable in the sub-


sequent discussions. We will discuss how to solve the corresponding two jugs
problem algorithmically.

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.

By applying Algorithm 2.2, we can obtain the following integer sequence


(M1 ):
0 −→ 3 −→ 6 −→ 1 −→ 4
+3 +3 −5 +3
There are 3 additions and 1 subtraction involved, so x = 3, y = −1 is a so-
lution of the Diophantine equation 3x + 5y = 4. The numbers in the integer
sequence represent the total amounts of water inside the jugs at each step, and
the physical meaning of x = 3, y = −1 is that the 3-litre jug has been com-
pletely filled for 3 times and the 5-litre jug has been completely unfilled for 1
time.

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:

(0, 0) −→ (3, 0) −→ (0, 3) −→ (3, 3) −→ (1, 5) −→


(1, 0) −→ (0, 1) −→ (3, 1) −→ (0, 4)

Thus, the total number of pouring steps involved in M1 is 8.

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:

(0, 0) −→ (0, 5) −→ (3, 2) −→ (0, 2) −→ (2, 0) −→


(2, 5) −→ (3, 4)
On algorithmic approach to the two jugs problem 1791

Thus, the total number of pouring steps involved in M2 is 6, which is 2 steps


less than that of M1 .

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.

By applying Algorithm 2.2, we can obtain the following integer sequence


(M1 ):
0 −→ 3 −→ 6 −→ 9 −→ 2
+3 +3 +3 −7

There are 3 additions and 1 subtraction involved, so x = 3, y = −1 is a solu-


tion of the Diophantine equation 3x + 7y = 2. It means the 3-litre jug has been
completely filled for 3 times and the 7-litre jug has been completely unfilled for
1 time.

The corresponding water pouring sequence is like this:

(0, 0) −→ (3, 0) −→ (0, 3) −→ (3, 3) −→ (0, 6) −→ (3, 6)


−→ (2, 7)

Thus, the total number of pouring steps involved in M1 is 6.

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

There are 2 additions and 4 subtractions involved, so x = −4, y = 2 is a solu-


tion to the equation 3x + 7y = 2. It means the 3-litre jug has been completely
unfilled for 4 times and the 7-litre jug has been completely filled for 2 times.

The corresponding water pouring sequence is as follows:

(0, 0) −→ (0, 7) −→ (3, 4) −→ (0, 4) −→ (3, 1) −→


(0, 1) −→ (1, 0) −→ (1, 7) −→ (3, 5) −→ (0, 5) −→
(3, 2)

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

4 Determining the Optimal Solution


We have shown how to apply the Algorithm 2.2 or Algorithm 2.3 to obtain a
special solution, say x = x0 , y = y0 , of the Diophantine equation mx + ny = d
and hence the water pouring sequence to obtain the quantity of water required.
We now discuss how to determine the optimal solution of the general two jugs
problem.

Definition 4.1. Let x be a real number. The floor of x is defined as:

bxc = max{m ∈ Z | m ≤ x}

Definition 4.2. Let x be a real number. The ceiling of x is defined as:

dxe = min{m ∈ Z | m ≥ x}

Definition 4.3. Let P (x, y) be a point on the x,y-plane. We define the


norm kP k to be the distance between P and the origin,namely:
p
kP k = x2 + y 2

As illustrated by the previous examples, the solutions (x, y) of the Dio-


phantine equation mx + ny = d are closely related to the total number of
pouring steps of the associated two jugs problem. The optimal solution of the
general two jugs problem can be defined as follows.

Definition 4.4. If P (x0 , y0 ) is a solution of the Diophantine equation mx+


ny = d such that kP k ≤ kQk for any possible solution Q of the Diophantine
equation, then the corresponding water pouring sequence of P is called the
optimal solution of the associated two jugs problem.

The following result describes the general solution of mx + ny = d. Reader


can refer to [9] for its proof.

Theorem 4.5. If x = x0 , y = y0 is a particular solution of the Diophantine


equation mx + ny = d, then the general solution of the equation is given by:

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

Theorem 4.6. If (x0 , y0 ) is a particular solution of the Diophantine equa-


tion mx + ny = d, then the water pouring sequence determined by P (x0 , y 0 )
provides an optimal solution to the associated two jugs problem, where x0 , y 0
can be obtained as follows:

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

Let z = `(nx0 − my0 )/(m2 + n2 ). Then, kP k is minimum when k̂ − z is least


possible. Since k̂ is an integer and bzc ≤ z ≤ dze, there are two cases to be
considered, namely:
Case(i): If z ≥ (bzc + dze)/2, then z − bzc ≥ dze − z. For all integer k,
|k − z| ≥ min{dze − z, z − bzc} = dze − z. So, |k̂ − z| is least if k̂ is chosen to
be dze.
Case (ii): If z < (bzc + dze)/2, then z − bzc < dze − z. For all integer k,
|k − z| ≥ min{dze − z, z − bzc} = z − bzc. So, |k̂ − z| is least if k̂ is chosen to
be bzc.
It completes the proof.

We now illustrate how to apply Theorem 4.6 to the previous examples.


1794 Yiu-Kwong Man

Example 4.7. In Example 1, we have m = 3, n = 5, d = 4, ` = gcd(3, 5) =


1 and the associated Diophantine equation is 3x + 5y = 4. By Algorithm 2.2,
we can obtain a particular solution x0 = 3, y0 = −1. Now,

nx0 − my0 5(3) − 3(−1) 18


z =`· 2 2
= 2 2
= .
m +n 3 +5 34
Since z ≥ (bzc + dze)/2 = 21 , so k̂ = dze = 1. By Theorem 4.5, we have:
5 3
x0 = 3 − = −2, y 0 = −1 + = 2.
1 1
Hence, the water pouring sequence determined by P (−2, 2) provides an opti-
mal solution to the associated two jugs problem, which is consistent with the
conclusion given in Example 1.

Note: If we apply Algorithm 2.3 to obtain the particular solution x0 = −2,


y0 = 2, then z = −4/34 and k̂ = dze = 0. Hence, x0 = −2, y 0 = 2 and the
conclusion is the same as before.

Example 4.8. In Example 2, we have m = 3, n = 7, d = 2, ` = gcd(3, 7) =


1 and the associated Diophantine equation is 3x + 7y = 2. By Algorithm 2.2,
we can obtain a particular solution x0 = 3, y0 = −1. Now,

nx0 − my0 7(3) − 3(−1) 24


z =`· 2 2
= 2 2
= .
m +n 3 +7 58
Since z < (bzc + dze)/2 = 21 , so k̂ = bzc = 0. By Theorem 4.5, we have:

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.

Note: If we apply Algorithm 2.3 to obtain the particular solution x0 = −4,


y0 = 2, then z = −34/58 and k̂ = bzc = −1. Hence, x0 = −4 + 7 = 3,
y 0 = 2 − 3 = −1 and the conclusion is the same as before.

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.

[2] H. S. M. Coxeter, S. L. Greitzer Geometry Revisited, The Mathematical


Association of America , Washington, 1967.

[3] H. E. Dudeney, Amusements in Mathematics, Dover, New York, 1970.

[4] M. E. Atword, Further explorations with a process model for water jug
problems, Mem. Cognition, 8 (1970), 182 - 192.

[5] A. Aggarwal, R. Anderson, A random NC algorithm for depth first


search, Proceedings of 19th annual ACM symposium on Theory of Com-
put., (1987), 325 - 334. http://dx.doi.org/10.1145/28395.28430

[6] C. J. McDiarmid, J. R. Alfonsin, Sharing jugs of wine, Discrete Math.,


125 (1994), 279 - 287. http://dx.doi.org/10.1016/0012-365x(94)90169-4

[7] B. Harvey, Computer Science Logo Style: Symbolic Computing, Vol. I,


MIT, Massachusetts, 1997.

[8] M. K. Colvin, K. Dunbar, J. Grafman, The effects of frontal lobe lesions


on goal achievement in the water jug task, J. Cognitive Neurosci., 13
(2001), 1129 - 1147. http://dx.doi.org/10.1162/089892901753294419

[9] D. M. Burdon, Elementary Number Theory, 5th ed., McGraw-Hill, New


York, 2002.

[10] F. Leon, M. H. Zaharia, D. Galea, A heuristic for solving the generalized


water jugs problem, Bull. Poly. Institute IASI, 1-4 (2005), 95 - 102.

[11] S. L. Beilock, M.S. DeCaro, From poor performance to success un-


der stress: working memory, strategy selection and mathematical prob-
lem solving under pressure, J. Exp. Psychol., 33 (2007), 983 - 998.
http://dx.doi.org/10.1037/0278-7393.33.6.983

[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

[13] H. P. Carder, S. J. Handley, T. J. Perfect, Counterintuitive and alternative


moves choice in the water jug tasks, Brain Cognition, 66 (2008), 11 - 20.
http://dx.doi.org/10.1016/j.bandc.2007.04.006

[14] S. A. Naser, Developing visualization tool for the teaching AI


searching algorithms, Inform. Technol. J., 7 (2008), 350 - 355.
http://dx.doi.org/10.3923/itj.2008.350.355

[15] A. Kolovou, M. Heuvel-Panhuizen, A. Bakker, Non-routine problem solv-


ing tasks in primary school mathematics textbooks - A needle in a
haystack, Mediterranean J. Research in Math. Educ., 8 (2009), 31 - 68.

[16] J. Dindyal, T. L. Toh, K. S. Quek, Y. H. Leong, Reconceptualising prob-


lem solving in the school curriculum, Proceedings of 32nd annual confer-
ence of the Math. Educ., Vol. 1, (2009), 682 - 685.

[17] Y. K. Man, Solving the water jugs problem by an integer sequence


approach, Int. J. Math. Educ. Sci. Technol., 43 (2012), 109 - 113.
http://dx.doi.org/10.1080/0020739x.2011.562323

[18] Y. K. Man, A non-heuristic approach to the general two water jugs prob-
lem, J. Commun. Comput., 10 (2013), 904 - 908.

Received: February 5, 2015; Published: March 9, 2015

You might also like