dark mode light mode Search Menu
Search

Sailors, Coconuts, and a Monkey

Phillip Capper on Flickr

This puzzle mixes math and coding. Plus you can go online to try the code yourself.

In his book The Colossal Book of Mathematics: Classic Puzzles, Paradoxes, and Problems, Martin Gardner simplified a classic question about monkeys and coconuts. His simplified version goes as follows:

“Three sailors come upon a pile of coconuts. The first sailor takes half of them plus half a coconut. The second sailor takes half of what is left, plus half a coconut. The third sailor also takes half of what remains, plus half a coconut. Left over is exactly one coconut, which they toss to the monkey. How many coconuts were in the original pile? If you arm yourself with 20 matches, you will have ample material for a trial-and-error solution.”

To get a feel for the problem, let’s guess that the sailors collect 9 coconuts.

  1. That means the first sailor takes half of 9 (4.5) plus .5 more for a total of 5 coconuts. How many coconuts are left for the second sailor?
  2. Well, that would be the total minus what Sailor 1 took, 9 – 5 = 4.
  3. The second sailor takes half of the 4 (2) plus ½ more, making 2.5. That means he leaves 4 – 2.5, or 1.5 coconuts for the 3rd sailor.
  4. Finally, Sailor 3 takes half of 1.5 (.75) plus 0.5 coconuts, for a total of 1.25.
  5. If Sailor 3 started with 1.5 coconuts and took 1.25 coconuts, there is ¼ of a coconut left. But that is problematic since the question says that at this point we should have exactly one whole coconut remaining to toss to the monkey.

We could make a second guess, and then possibly and third and so on until we end up with 1 coconut for the monkey. However, we are likely to make mistakes along the way, or just get frustrated and quit.

Mr. Gardner recommends arming oneself with 20 matches, implying the answer is no more than 20, but how about we arm ourselves with a little code as well?

Since we understand the process of dividing up the coconuts for each sailor, we can write a program to do the calculations for us. We will still need to make a guess, but the computer can do the calculations and tell us if our next guess should be higher or lower. We’ll give this a try in Python.

This program gets us the answer, but leave plenty of room for improvement. If you have a bit of programming experience, you can challenge yourself to rewrite them with fewer lines of code, or by defining functions.

The answer might be much more than 20, so you’ll probably want a program that makes all the guesses for you.

Here’s the process in pseudo-code:

  1. Guess a number
  2. Divide it by 2 and add ½
  3. Subtract the answer from our original guess
  4. Repeat the process two more times

And here’s the pseudo-code written in Python 3 code to try at the repl.it website:

#our first guess is 17, you can replace 17 with your own guess
total = float(17)

#we want to figure out how many coconuts all 3 sailors get, so we need to loop through the algorithm 3 times. You can replace the numbers in range(1,4) with any two numbers that are 3 apart.

for i in range(1,4):
    sailorTakes = total/2 + .5
    print(“Sailor”,i, “takes”, sailorTakes)
    remaining = total - sailorTakes
    total = remaining
    print(“There are”, total, “left”)
    print(“-------------------”)

if(total == 1):
    print(“You got it!”)
elif(total > 1):
    print(“Guess a smaller number”)
else:
    print(“Guess a bigger number”)

Learn More

Project Code in Repl.it

https://repl.it/FyJE/7

The Colossal Book of Mathematics: Classic Puzzles, Paradoxes, and Problems

http://books.wwnorton.com/books/978-0-393-02023-6/
http://www.indiebound.org/book/9780393020236
http://www.alibris.com/The-Colossal-Book-of-Mathematics-Classic-Puzzles-Paradoxes-and-Problems-Martin-Gardner/book/28766065

Martin Gardner’s Puzzle Books

http://martin-gardner.org/PuzzleBooks.html