## Sailors, Coconuts, and a Monkey

Can you figure out how to divide up coconuts between a group of sailors and a monkey? This puzzle mixes math and coding. Plus you can go online to try the code yourself.

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.

- 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 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:

- Guess a number
- Divide it by 2 and add ½
- Subtract the answer from our original guess
- 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

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

### Also In The February 2020 Issue

Can you figure out how to divide up coconuts between a group of sailors and a monkey? This puzzle mixes math and coding. Plus you can go online to try the code yourself!

Recreate the classic game in this simple Python tutorial. What whimsical stories can you write?

If you like ships, then you’ll love this easy-to-use website that keeps track of seafaring vessels around the world. Bonus: it helps prevent maritime collisions!

Ready for some good old-fashioned winter fun? In this article, build a digital snowman with Sketchup.

A fun, silly way to share your coding trials and triumphs with friends — because everything is better with kittens!

Should you learn Python, Scratch, Java, Assembly? If you’re feeling overwhelmed by too many options, this article is here to help.

Illustrating computational concepts like decomposition and algorithms with simple, hands-on, and occasionally messy activities.

How do street lights know when to turn on? And what’s in store for the street lights of the future?

In the old days, before video game systems had cameras and sensors, programmers had to get creative.

Six women were hired to use their math skills to program the ENIAC computer. They called themselves The First Programmers Club.

Learn about the key software that keeps your computer safe from viruses.

Programs are constantly being patched and improved. How do we keep track of all this new code?

Dive into the nitty-gritty details of binary numbers: how they work, why they’re used, and where they come from.

Binary might be the language of the computer, but we humans can use it to decode secret messages.

An easy way to code your own 3D graphics online. Dive into the world of pixels, triangles, textures, and colours!

Learn about the smallest, simplest computers and where they’re still used today.

Why Smalltalk is such a unique language, and how it evolved into modern variants like Scratch, Squeak, and Pharo.

Interesting stories about science and technology for February 2020.

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