Welcome, guest | Sign In | My Account | Store | Cart

Five sailors arrive at a deserted island that has only coconuts and one monkey. The sailors collect all the coconuts into one big pile and agree to divide up the coconuts into equal shares the next morning. However during the night each sailor wakes up one at a time afraid to trust the others and decides to take his share secretly. So each sailor takes 1/5 of the coconuts and hides it. Each time there is one coconut left over and the sailor gives that to the monkey. In the morning they divide what is left of the pile into equal shares and there is still one coconut left for the monkey.

How many coconuts were in the original pile?

Python, 29 lines
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
NUM_SAILORS = 5
NUM_ITERATIONS = 6
UPPER_BOUND = 1000000

def check(pile, verbose=False):
    for i in range(0, NUM_ITERATIONS):
        share, monkey = divmod(pile, NUM_SAILORS)
        if monkey == 1:
            new_pile = pile - (share + monkey)
            if verbose:
                print ("%d: share [%d] monkey [%d] new_pile [%d]" % (pile, share, monkey, new_pile))
            pile = new_pile
        else:
            return False
    return True   

def solve(upper_bound):
    for x in range(1, upper_bound):
        if check(x):
            return x
    return 0

if __name__ == "__main__":
    x = solve(UPPER_BOUND)
    if x:
        print ("Solution: %d" % x)
        check(x, True)
    else:
        print ("No solution < %d" % UPPER_BOUND)

In 1961 Martin Gardner posed this problem in his "Scientific American" Mathematical Games column and received a record-breaking reader response.

Several years later my father gave me the problem to work on. I was only in 7th grade and I didn't get very far aside from realizing that the last digit of the coconuts number had to be one or six. Eventually I found the answer in the back issues of SciAm at a local library.

Last night I was curious how hard it would be to brute force an answer using Python. I was surprised at how quickly the code came together and how trivial it was for a modern personal computer. I was also pleased that converting the word problem to code made it much more clear.

For a mathematical solution: http://mathworld.wolfram.com/MonkeyandCoconutProblem.html

2 comments

AJ. Mayorga 13 years, 8 months ago  # | flag

Interesting problem heres my shot at it. Let me know if it's off any

import sys

l     = 0      # Leftover Pile
m     = 5      # Sailors 
ms    = 1      # Monkey_share
pile  = m+ms*2 # Reasonable Start Point

limit = 100000 # Reasonable High Limit

for l in [l for l in range(limit) if l % m == ms and l > m+ms]:
    while 1:
        p = pile
        for x in range(m):
            p -= (p/m)+ms
        if p == l:
            print "Original Coconut Pile ",pile
            sys.exit()
        pile += 1
Jack Trainor (author) 13 years, 7 months ago  # | flag

Well, no. When I run your code, it yields 37, which fails on the first iteration since the first remainder is 2 coconuts, not 1 for the monkey.

The correct solution is 15,621. If you run my code you can see the breakdown on each round of taking away 1/5 of the coconuts and giving one to the monkey.

I can't tell what's going on in the first line of your 'for' statement, but the main problem I see is that you are testing for a pile of one at the end rather than a remainder of one at each iteration. Plus there should six iterations, not five.

Thanks for taking a whack at this one. It quite mystified me and a lot of people when Martin Gardner posed it.

Created by Jack Trainor on Mon, 26 Jul 2010 (MIT)
Python recipes (4591)
Jack Trainor's recipes (33)

Required Modules

  • (none specified)

Other Information and Tasks