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