## David Gries’ Coffee Can Problem

### October 22, 2013

Each time you reach into the coffee can, you remove two beans then put one in, so the number of beans in the can is reduced by one and the process must eventually terminate when one bean remains. Since either zero or two white beans are removed each time you reach into the coffee can, the even/odd parity of the number of white beans never changes, so the final remaining bean is white if and only if there were initially an odd number of white beans in the coffee can. Here is our simulation:

`(define (coffee b w)`

(if (and (zero? b) (= w 1)) 'w

(if (and (zero? w) (= b 1)) 'b

(if (and (zero? b) (= w 2)) 'b

(if (and (zero? w) (= b 2)) 'b

(if (and (= b 1) (= w 1)) 'w

(let* ((one (if (< (randint (+ b w)) b) 'b 'w))

(b (if (eq? one 'b) (- b 1) b))

(w (if (eq? one 'w) (- w 1) w))

(two (if (< (randint (+ b w)) b) 'b 'w))

(b (if (eq? two 'b) (- b 1) b))

(w (if (eq? two 'w) (- w 1) w)))

(if (eq? one two)

(coffee (+ b 1) w)

(coffee b (+ w 1))))))))))

We handle all combinations of two or less beans as special cases because the main calculation in the `let*`

accesses two beans. Here are some examples:

`> (coffee 5 8)`

b

> (coffee 5 7)

w

We can demonstrate both that our code is correct and that our statement about the color of the remaining bean is correct by testing all combinations of up to 50 beans:

`> (do ((b 1 (+ b 1))) ((= b 50))`

(do ((w 1 (+ w 1))) ((= w 50))

(if (even? w)

(assert (coffee b w) 'b)

(assert (coffee b w) 'w))))

>

The test prints nothing, on the theory that no news is good news. We used `randint`

and `assert`

from the Standard Prelude. You can run the program at http://programmingpraxis.codepad.org/qEpegqCq.

A solution in Racket and a proof by induction in “engrish”:

I went a slightly different route, both with language (C++11) and simulation

technique. As for the problems, the second one tripped me up. Moreover, it

reminded me of a similar problem in Hofstadter’s “Godel, Escher, Bach” that

also stumped me a bit. You’d think I would have learned my lesson.

Solution in ruby

WHITE = ‘W’

BLACK = ‘B’

class CoffeCan

def initialize

@beans = []

end

def add_bean(bean)

@beans.push bean

end

def fill_randomly(size)

amount = rand size

(0..amount).each do

if (rand 2) == 0

@beans.push WHITE

else

@beans.push BLACK

end

end

end

def remove_two_beans

[@beans.delete_at(rand(@beans.size)), @beans.delete_at(rand(@beans.size))]

end

def is_possible

(@beans.size > 1) || false

end

def to_s

@beans.join ‘ ‘

end

end

class SimulateCan

def run (size=10000)

can = CoffeCan.new

can.fill_randomly size

puts can.to_s

while can.is_possible

beans = can.remove_two_beans

if beans[0] == beans[1]

can.add_bean BLACK

else

can.add_bean WHITE

end

puts can.to_s

end

end

end

sim = SimulateCan.new

sim.run

First, clearly this problem calls for a coffee-associated programming language. As I find that I need to learn some Coffeescript anyway, here’s my first ever Coffeescript program.

Second, while the randomness specification is a red herring, let’s at least make it more realistic: my replacement step shuffles some number of the beans on top, not the whole can. To do this, I represent the can as an array of distinct beans. I also take care to return the same white bean to the can (another red herring in the specification).

I omit the Fisher-Yates shuffle code that I found on the web.

replace = (can) ->

[a, b] = [can.shift(), can.shift()]

if a[0] is b[0] then can.unshift(['black'])

else can.unshift(if a[0] is 'white' then a else b)

top = Math.min(some, can.length)

can[0...top] = shuffle(can[0...top])

can

`process = (can) ->`

while can.length > 1

replace(can)

To make and shuffle an initial can, run the process on it with the number of shuffled top beans specified, and print the final can, I had the following. It appears to print a can with a white bean in it when there is an odd initial number of white beans.

some = 8

whites = 39

beans = 41

can = ([if k < whites then 'white' else 'black'] for k in [0...beans])

shuffle(can)

process(can)

console.log(can)

This was fun.

Is this an acceptable simulation of the problem in Java?

Mark, I think it would be better if the simulation could be run with different numbers of beans and different proportions of white and black. Your program generates only cans (of 100000, quite a lot) with roughly equal proportions of the colours.

It would also be appropriate to print the initial numbers of white and black beans together with the colour of the final bean. Or let the user supply the initial numbers.

It’s unrealistic that every bean is equally likely to be picked but the framers of the problem didn’t have such realism in mind. The original framers didn’t even have simulation in mind. It was about reasoning.

And yes, Java is an appropriate language for coffee-can problems.