The Traveller and Three Kegs

There are 3 kegs of water. The first keg has 1 liter, the second keg has 2 liters, and the third keg has 3 liters of water in it.

Each day our traveler picks one keg at random with each keg having the same chance of being picked, and drinks one liter of water from it. When he empties a keg, he throws it away. When he is left with one keg, he records the amount of water in it.

What is the expected amount of water (in liters) remaining in that last keg?

If this expected value can be expressed as a b \dfrac ab , where a a and b b are coprime positive integers, enter a + b a+b .


Note: You may use a calculator if you want.

Bonus: Solve this problem for 5 kegs.


Inspired by bobbym .


The answer is 197.

This section requires Javascript.
You are seeing this because something didn't load right. We suggest you, (a) try refreshing the page, (b) enabling javascript if it is disabled on your browser and, finally, (c) loading the non-javascript version of this page . We're sorry about the hassle.

4 solutions

I wish I could say that I've found a great way to solve this problem, but I have not.

The way to calculate the expected value of the water in the last keg is to actually trace all possible ways to reach the last keg weighted by their probabilities and sum them up.

Also I will use a rational number library to store this value as a rational number, so as to not loose out on accuracy.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
import Data.List
import Data.Ratio

type Keg = Rational
type State = [Keg]

rots :: [a] -> [[a]]
rots xs = init (zipWith (++) (tails xs) (inits xs))

calculate :: State -> Rational
calculate kegs = go 0.0 1.0 kegs
    where
        go :: Rational -> Rational -> State -> Rational
        go e p [w]  = e + p*w
        go e p kegs         = foldr (\st e' -> go e' p' st) e newStates
            where
                newStates = [ f s | s <- (rots kegs) ]
                f (1.0:ks) = ks
                f (w:ks) = (w-1):ks
                p' = p/(fromIntegral $ length newStates)

ans = calculate [1,2,3]


Also, I used a monte-carlo simulation to double check my answer:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
from random import choice

def simulate(kegs):
    while len(kegs) > 1:
        n = choice(range(len(kegs)))
        kegs[n] -= 1;
        if kegs[n] == 0:
            del kegs[n]
    return kegs[0]

ans = sum((simulate([1,2,3]) for i in range(1000000)))/1000000

It's an insane answer

Aniket Sharma - 4 years, 4 months ago

Log in to reply

Insane in a good way?

Agnishom Chattopadhyay - 4 years, 4 months ago

its a very good answer but I wasnt able to understand your graph, can you please elaborate?

Nishant Sood - 4 years, 4 months ago

Log in to reply

Sure. Let me try to explain that by asking a counter-question.

How would you be tackling this problem for 2 kegs?

Agnishom Chattopadhyay - 4 years, 4 months ago

Log in to reply

@Agnishom Chattopadhyay ok, in this case there are basically just 2 ways it could start, if he happens to pick up the one with just 1 ltr then he will finish n throw it, if he picks up the second one then he drinks 1tr and conserves the bottle as it will still have 1 ltr but whats puzzleing me is that hes not going to calculate the water leftover till he reaches last bottle!

Nishant Sood - 4 years, 4 months ago

Log in to reply

@Nishant Sood yes, and can you do a little tree diagram for this?

Agnishom Chattopadhyay - 4 years, 4 months ago

Log in to reply

@Agnishom Chattopadhyay 1 -- 2 ---1, 2---1, 1 --1 ,this is the best I could do ,in replies they wont let you do anything serious!

Nishant Sood - 4 years, 4 months ago

Log in to reply

@Nishant Sood And the corresponding tree is this, right:

Agnishom Chattopadhyay - 4 years, 4 months ago

That was nice. I think TT.TT

Manuel Kahayon - 4 years, 4 months ago

Log in to reply

Sorry, what is TTTT?

Agnishom Chattopadhyay - 4 years, 4 months ago

Log in to reply

It's a crying face :p T.T extended :D

Manuel Kahayon - 4 years, 4 months ago

Anyways, it was really intense, but really nice at the same time.

Manuel Kahayon - 4 years, 4 months ago

Yes! @Agnishom Chattopadhyay

Aniket Sharma - 4 years, 4 months ago

Is there a pattern to the value of f ( a , b , c ) f(a, b, c) ? If so, we could prove it by induction.

Calvin Lin Staff - 4 years, 4 months ago

Log in to reply

I cannot think of any recursion here.

Agnishom Chattopadhyay - 4 years, 4 months ago

Log in to reply

In your solution, can you add in the values of f ( a , b , c ) f(a, b, c) for when these are 3 \leq 3 ? That might lead others who are interested to guess at a pattern.

(Oh, and I just realized I'm committing the mistake of double notation, given that a , b a, b were used in the question. Oh well, it should be obvious what I'm saying in the comments.

Calvin Lin Staff - 4 years, 4 months ago
Abdelhamid Saadi
Jan 19, 2017

With the same method as @Agnishom, but in python

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
"The Traveller and Three Kegs"

from fractions import Fraction as F

def expct(lst, w):
    lst0 = [x for x in lst if x != 0]
    n = len(lst0)
    if  n == 0:
        return 0
    elif n == 1:
        return lst0[0]*w
    else:
        return sum(expct(lst0[0:i]+[lst0[i] - 1] + lst0[i+1:], w/n) for i in range(n))

print(expct([1,2,3], F(1,1)))

Output:

1
125/72

For the bonus, we need a more optimized solution. We'll be here soon

Hmm, I was thinking if we could improve a little by caching the values?

Agnishom Chattopadhyay - 4 years, 4 months ago
Dmitry Nikolaev
Jun 26, 2017

A code snippet in Julia to calculate the probability of any final configuration recursively (unoptimised; we should add memoisation for larger problems):

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
function prb(p, q, r)
    if p > 1 || q > 2 || r > 3
       return 0
    elseif p == 1 && q == 2 && r == 3
       return 1
    end
    if p == 0 && q == 0
       return 1//2 * prb(1,0,r) + 1//2 * prb(0,1,r)
    elseif p == 0 && r == 0
       return 1//2 * prb(1,q,0) + 1//2 * prb(0,q,1)
    elseif q == 0 && r == 0
       return 1//2 * prb(p,1,0) + 1//2 * prb(p,0,1)
    elseif p == 0
       return 1//3 * prb(1,q,r) + 1//2 * prb(0,q+1,r) + 1//2 * prb(0,q,r+1)
    elseif q == 0
       return 1//3 * prb(p,1,r) + 1//2 * prb(p+1,0,r) + 1//2 * prb(p,0,r+1)
    elseif r == 0
       return 1//3 * prb(p,q,1) + 1//2 * prb(p+1,q,0) + 1//2 * prb(p,q+1,0)
    else
       return 1//3 * prb(p+1,q,r) + 1//3 * prb(p,q+1,r) + 1//3 * prb(p,q,r+1)
    end
end

Judy Gu
May 3, 2017

The amount of water(in liters) left in the one last keg has 3 possible values: 1, 2, and 3. Let P(n)=the probability that the amount of water in the keg is n(n=1, 2 or 3). To get the expected amount of water, we need to determine 1 * P(1) + 2 * P(2) + 3 * P(3) .

Note that if there is water in all the 3 kegs, the probability of picking any keg at random is 1 3 \frac{1}{3} . Once a keg is emptied and thrown away, however, the probability of picking a keg becomes 1 2 \frac{1}{2} . In my solution, there are a lot of notations such as p(1-2-2) . This represents the probability of picking keg # on day 1, 2, 3. etc. For example, p(1-2-3-2) is the probability that the traveler picks the 1st, 2nd, 3rd and 2nd keg on day 1, 2, 3 and 4 respectively.

I. P(3) --- Let's calculate P(3) first since it's the most straightforward of all. The only last keg which could have 3 liters of water left is the 3rd keg . Therefore, P(3) = p(1-2-2) + p(2-1-2) + p(2-2-1) = 1 3 \frac{1}{3} * 1 2 \frac{1}{2} * 1 2 \frac{1}{2} + 1 3 \frac{1}{3} * 1 3 \frac{1}{3} * 1 2 \frac{1}{2} + 1 3 \frac{1}{3} * 1 3 \frac{1}{3} * 1 2 \frac{1}{2} = 7 36 \frac{7}{36} .

II. P(2) --- If there are 2 liters of water left in the last keg, then it must be either the 2nd or the 3rd keg . We can break down the calculation into the following 2 scenarios:

a. 2 liters of water left and the last keg is the 2nd keg = p(1-3-3-3) + p(3-1-3-3) + p(3-3-1-3) + p(3-3-3-1) = 1 3 \frac{1}{3} * 1 2 \frac{1}{2} * 1 2 \frac{1}{2} * 1 2 \frac{1}{2} + 1 3 \frac{1}{3} * 1 3 \frac{1}{3} * 1 2 \frac{1}{2} * 1 2 \frac{1}{2} + 1 3 \frac{1}{3} * 1 3 \frac{1}{3} * 1 3 \frac{1}{3} * 1 2 \frac{1}{2} + 1 3 \frac{1}{3} * 1 3 \frac{1}{3} * 1 3 \frac{1}{3} * 1 2 \frac{1}{2} = 23 216 \frac{23}{216} .
b. 2 liters of water left and the last keg is the 3rd keg = p(1-2-3-2) + p(1-3-2-2) + p(2-1-3-2) + p(2-3-1-2) + p(2-2-3-1) + p(2-3-2-1) + p(3-1-2-2) + p(3-2-1-2) + p(3-2-2-1) = 1 3 \frac{1}{3} * 1 2 \frac{1}{2} * 1 2 \frac{1}{2} * 1 2 \frac{1}{2} + 1 3 \frac{1}{3} * 1 2 \frac{1}{2} * 1 2 \frac{1}{2} * 1 2 \frac{1}{2} + 1 3 \frac{1}{3} * 1 3 \frac{1}{3} * 1 2 \frac{1}{2} * 1 2 \frac{1}{2} + 1 3 \frac{1}{3} * 1 3 \frac{1}{3} * 1 3 \frac{1}{3} * 1 2 \frac{1}{2} + 1 3 \frac{1}{3} * 1 3 \frac{1}{3} * 1 2 \frac{1}{2} * 1 2 \frac{1}{2} + 1 3 \frac{1}{3} * 1 3 \frac{1}{3} * 1 3 \frac{1}{3} * 1 2 \frac{1}{2} + 1 3 \frac{1}{3} * 1 3 \frac{1}{3} * 1 2 \frac{1}{2} * 1 2 \frac{1}{2} + 1 3 \frac{1}{3} * 1 3 \frac{1}{3} * 1 3 \frac{1}{3} * 1 2 \frac{1}{2} + 1 3 \frac{1}{3} * 1 3 \frac{1}{3} * 1 3 \frac{1}{3} * 1 2 \frac{1}{2} = 13 54 \frac{13}{54} .

Now, add the two fractions in a and b, we get P(2) = 23 216 \frac{23}{216} + 13 54 \frac{13}{54} = 25 72 \frac{25}{72} .

III. P(1) --- If there is only 1 liter of water left, it could be in any of the 3 kegs. If we try to dig out all the possible situations, the calculation will get convoluted. However, there is a trick! Since 1, 2 and 3 are the only possible values, then P(1) + P(2) + P(3) must be equal to one! In other words, P(1) = 1 - P(2) - P(3). Therefore, in this case, P(1) = 1 - 7 36 \frac{7}{36} - 25 72 \frac{25}{72} = 11 24 \frac{11}{24} .

Now the expected amount of water = 1 * 11 24 \frac{11}{24} + 2 * 25 72 \frac{25}{72} + 3 * 7 36 \frac{7}{36} = 125 72 \frac{125}{72} . Since 125 and 72 are coprime positive integers, the final answer is: 125 + 72 = 197 \boxed{197}

The calculations are a little simpler if we only consider the expected value of water left. Let E(a,b,c) be the expected amount of water left starting with a/b/c liters in the 3 kegs. If none of a, b, c are zero, then E(a,b,c) = 1/3 E(a-1,b,c) + 1/3 E(a,b-1,c) + 1/3 E(a,b,c-1). Also, E(0,b,c) = 1/2 E(0,b-1,c) + 1/2*E(b,c-1) and E(0,0,c) = c. Keeping in mind that E(a,b,c) is independent of the order of its arguments, it takes a few minutes with pencil and paper to calculate E(1,2,3) = 125/72, giving the required answer of 197.

Fredric Kardon - 3 years, 7 months ago

0 pending reports

×

Problem Loading...

Note Loading...

Set Loading...