## Monkeying Around

After a huge storm blew up, the ship foundered. Dawn’s light found just six survivors washed up on a desert island – five men and one monkey. The men spent the day gathering all the food they could find – which was just a pile of coconuts. After a dinner of coconut milk and flesh, the exhausted men decided to they would divide the remaining coconuts up the following day, before splitting up and exploring the remainder of the island.

During the night one man woke up and started worrying about whether there would be a disagreement in the morning over the coconuts. He decided to take his share, and divided the coconuts equally into five. There was one left over which he gave to the monkey, then he took his share and hid them away, before going back to sleep.

A little later the next man woke up, and had the same fear. He too divided the coconuts into five, and had one left over that he gave to the monkey, before hiding his share and going back to sleep. And so the night went on, with each man waking up, dividing the remaining coconuts in five, hiding one share and giving the one extra to the monkey.

In the morning, the men awoke and divided the remaining coconuts, which came out to five equal shares. No-one said anything about the missing coconuts as each thought he had been the first to hide a share and was thus the best off.

How many coconuts were there in the beginning?

It is not difficult to express this problem as a set of equations; what makes the equations hard to solve is that the solutions must be integers. Such equations are called Diophantine equations, after the 3rd century AD’s Diophantus of Alexandria, who was the first known algebraist to examine equations limited to rational number solutions. We know little of Diophantus himself other than the following puzzle left after his death:

- his childhood lasted 1/6 of his life
- he grew a beard after another 1/12
- after 1/7 more he married
- 5 years later he had a son
- the son lived to half Diophantus’ age
- and Diophantus died 4 years later

From this we can determine how long Diophantus lived, how long his boyhood lasted, what age he grew a beard, what age he got married, when he had a son, and how long his son lived.

Diophantus’ great work, The Arithmetica, is a text on computational arithmetic rather than theoretical mathematics, closer to the mathematics of Egypt, Babylon and India than to Greece. Diophantus promised 13 books in his introduction but only ten are known, with four only being discovered recently in an Arabic manuscript found in Iran. The earliest commentaries on The Arithmetica were written by Hypatia, the first known Western female mathematician, who was brutally murdered by a mob of Christian fanatics in 415 AD as they burned Alexandria’s great library to the ground and destroyed much of the world’s repository of classical knowledge.

Many people are familiar with Fermat’s famous “Last Theorem”; it was in the margins of a copy of Diophantus’ Arithmetica (Book 2, problem 8 ), in which Diophantus describes how to find solutions to for a given value of, that Fermat wrote his famous note asserting that there are no solutions to equations of this form for any powers higher than two, and that “For this I have discovered a truly wonderful proof, but the margin is too small to contain it.”

Going back to the coconuts, if we call the initial number of coconuts and the number hidden away by each man as respectively, and the number of coconuts each sailor got on the next day as, we get this set of equations:

We can use successive substitutions to reduce this to a single equation in two unknowns. The first sailor took coconuts, and left coconuts. The second sailor then took coconuts and left coconuts. Continuing in this way we can find that the number of coconuts left by the third, fourth and fifth sailors were,, and respectively. Therefore:

or:

Normally, for real numbers, we would need two equations in two unknowns, but because we know the solutions must be integers the possible answers are more constrained. Equations of the form:

are known as linear Diophantine equations; in our case, and, and we have:

If and have a common denominator, then clearly for the equation to have integer solutions must also be a multiple of this common denominator.; if it is not, then there are no integer solutions. So we can simplify a linear Diophantine equation by dividing the coefficients of x and y, and the constant, by their greatest common denominator (GCD). The coefficients are then relatively prime, the gcd is 1 and no further simplification can be done.

Assuming we can simplify to the point where we have co-prime coefficients and still have an integer constant, we will have solutions – in fact, an infinity of them. This is quite easy to show. Assume we do have two solutions, and. Then:

and so:

So the difference between two solutions for must satisfy:

If is the GCD of and, then we can divide both and on the right hand side by to get a coprime numerator and denominator:

Because and are coprime, the smallest that can expand the right hand side out to an integer value is:

And similarly:

So we have an infinite number of solutions of the form:

The problem usually comes down to finding the smallest pair of positive integers that satisfy the equation.

To solve a linear Diophantine equation such as the one above, we can apply Euclid’s method for finding the GCD of the coefficients (which will be 1, as our coefficients are coprime, but we are interested in the steps anyway):

Note how the coefficients on the left of the right hand side match those in the continued fraction:

We can invert the steps to get a solution. Starting at the bottom:

We keep expanding this, working backwards, and substituting the equations we have derived above for the remainders:

So we have determined that and is a solution of. This is all very well but how do we adjust this for the fact that the value we actually care about is 8404, not 1? Simple! We just have to multiply each side by 8404 to get a solution to our original equation, namely and .

We know from earlier that the general solution of our equation has the form:

It’s easy to see we have our first positive solution at, namely and . So there were 3121 coconuts originally, and on the last division each sailor got 204 coconuts.

A variant of this problem has there being a single coconut left on the final day as well. In this case there is a very clever trick that can be used, which uses the fact that if we add four coconuts to the original pile it is evenly divisible by 5. If we allow the use of *negative* coconuts it is easy to see that –4 is a solution. In this case, the first sailor divides the pile into 5 negative coconuts, gives one positive coconut to the monkey, and returns four negative coconuts to the pile, so there are once again –4 coconuts! Each sailor repeats the same process, always leaving –4 coconuts. To get to the first positive solution we just need to observe that since we divide the pile into 5 six times, each successive solution must be larger than the last, so the first solution is 15621.

This variant appeared in a short story by Ben Ames Williams that ran in the Saturday Evening Post in October 1926. Williams did not include the answer, and the Post’s offices were flooded with letters, to the point where the editor sent Williams a wire: “For the love of Mike, how many coconuts? Hell popping around here”. Williams continued to receive letters requesting the answer for the next twenty years!

Further reading:

I first heard this problem, and learnt the method of continuous fractions, as a high school student when doing an extra-curricular math class at the University of the Witwatersrand’s Schmerenbeck Center (so long ago Diophantus may still have been alive). Martin Gardner’s Colossal Book of Mathematics discusses this problem in the first chapter, along with some unorthodox and general solutions. Steven Hawking’s book God created the Integers includes annotated excerpts from The Arithmetica, and some biographical material. Clifford Pickover’s The Math Book includes a chapter on Hypatia.

Nice post man! I like how you post the problems and give a couple of days before posting the solutions.

I cheated and solved it brute force but got a decent little example of the maybe monad out of it: http://blogs.msdn.com/ashleyf/archive/2009/12/14/monadic-piles-of-coconuts.aspx

AshleyFDecember 15, 2009 at 5:48 am

[…] a comment » In a previous post I wrote about how Fermat scribbled his famous “last theorem” in the margin of Diophantus’ […]

More on Diophantus and Fermat « MagiMathicsJanuary 3, 2010 at 8:33 pm

[…] Before doing so, it is worth revisiting Euclid’s clever algorithm for calculating the greatest common divisor (GCD) of two numbers. We made use of this when solving the Monkey and Coconuts puzzle. […]

Primal Soup « MagiMathicsJanuary 6, 2010 at 6:54 am

For a simpler solution, add 4 to each of the original equations:

x + 4 = 5(a + 1)

4(a + 1) = 5(b + 1)

4(b + 1) = 5(c + 1)

4(c + 1) = 5(d + 1)

4(d + 1) = 5(e + 1)

4(e + 1) = 5y + 4

Now, x + 4 = (5^5 / 4^5) (5y + 4), and so 5y + 4 = 4^5 is the smallest solution, and thus x = 5^5 – 4 = 3121.

NickJune 4, 2012 at 4:55 pm