beanz Magazine

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 us guess that the sailors collect 9 coconuts.

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?

Well, that would be the total minus what Sailor 1 took, 9 – 5 = 4.

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.

Finally, Sailor 3 takes half of 1.5 (.75) plus 0.5 coconuts, for a total of 1.25.

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 a mistake 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 the answer, but leave plenty of room of 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.

If you prefer drag and drop languages over text-based languages, try rewriting either algorithm in Scratch. And for those really out to impress, check the included link in the online version of this article for the original Monkey and Coconut question.

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 pseudo-code written up in Python (see link below to play with the code):

#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”)

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

Learn More

Project Code in

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

Martin Gardner’s Puzzle Books

Also In The April 2017 Issue

We all use fonts yet rarely notice they are designed. Here are some interesting details to help you notice fonts.

This iPad app is a creative tool kids can use to explore and record what they learn in school.

Smart software design makes it easy for you to learn how to use it without help.

A new version of a fun Mario-like game that teaches kids coding has been released. Learn HTML and save kittens!

Mark is a designer who also knows a lot about how to use technology to create design.

Design is about solving problems, from donuts to race cars, how we eat to what to wear in cold weather.

Everyone know the difference between saying, “Let’s eat, grandma!” and “Let’s eat grandma!” Computers don't.

The user interface often determines whether or not people can easily use your software.

State is an important concept in computer science as well as our everyday lives.

Links from the bottom of all the April 2017 articles, collected in one place for you to print, share, or bookmark.

Interesting stories about computer science, software programming, and technology for April 2017.

Computers collect garbage the way humans do. Here's how they manage memory space.

Code reviews help programmers improve their code and learn more about the software they build.