Home More Samples
```{
The Monkey and the Coconuts

Martin Gardner said that the Monkey and the Coconuts is "probably
the most worked on and least often solved" algebraic puzzle.
According to the OMNI magazine (April 1991), this puzzle caused
quite a commotion when it appeared in the October 9, 1926 Saturday Evening
Post as part of a short story, "Coconuts", by Ben Ames Williams.
Williams ended his story without solving the puzzle. It is said that some
2000 letters came to the Saturday Evening Post offices the week after
the "Coconuts" appeared, all requesting the puzzle solution.

Here is the puzzle itself:

Five sailors are shipwrecked on a desert island. They quickly determine
that the only other inhabitant of the island is a monkey and that the only
food is coconuts. They set about collecting as many coconuts as they can
and put them all in a pile. By nightfall they are too tired to divide the
harvest; so they agree to go to sleep and divvy up the coconuts the next
morning.
During the night one sailor awakens, suspicious that the others might try
to cheat him, and desides to take his portion then and there and not wait
until morning. He divides the coconuts into five piles and finds there is
one coconut left over, which he gives to the monkey. He hides one of the
five piles, then puts the rest of the nuts together and returns to sleep.
About an hour later a second sailor awakens with the same suspicions and
does the same thing: He divides the coconuts into five piles, leaving one
extra, which he gives to the monkey. Then he hides what he thinks is his
share and goes back to sleep.
One after another the rest of the sailors do the same: they each take one
fifth of the coconuts in the pile (after giving the extra one to the monkey)
When the sailors awaren the next morning they all notice the coconut pile
is much smaller than it was the night before, but since each man is as guilty
as the others, no one says anything. They divide the coconuts (for the sixth
time) and again there is one left for the monkey.

The question is: How many coconuts were in the original pile?

The program below solves the puzzle by asking the query

all SailorsMonkeyCoconuts(coconuts)

Note that there is an infinite number of solutions, the program finds all
solutions smaller than specified by the type declaration of Coconuts.
(The smallest positive number that solves the puzzle is 15621.)
}

Coconuts = L[0..50000]   // Extend the range for more solutions

pred SailorsMonkeyCoconuts(pile0::Coconuts) iff
// first sailor: divide the pile of the coconuts (pile0) into 5 parts
// (p1 each), give 1 coconut to the monkey, hide 1/5 of the coconuts,
// leaving the resulting pile (pile1) 4 parts of coconuts (4*p1)
p1::Coconuts & pile0 = 5*p1 + 1 & pile1 = 4*p1 &

// second sailor: divide the pile of the coconuts (pile1) into 5 parts
// (p2 each), give 1 coconut to the monkey, hide 1/5 of the coconuts,
// leaving the resulting pile (pile2) 4 parts of coconuts (4*p2)
p2::Coconuts & pile1 = 5*p2 + 1 & pile2 = 4*p2 &

// third sailor: divide the pile of the coconuts (pile2) into 5 parts
// (p3 each), give 1 coconut to the monkey, hide 1/5 of the coconuts,
// leaving the resulting pile (pile3) 4 parts of coconuts (4*p3)
p3::Coconuts & pile2 = 5*p3 + 1 & pile3 = 4*p3 &

// fourth sailor: divide the pile of the coconuts (pile3) into 5 parts
// (p4 each), give 1 coconut to the monkey, hide 1/5 of the coconuts,
// leaving the resulting pile (pile4) 4 parts of coconuts (4*p4)
p4::Coconuts & pile3 = 5*p4 + 1 & pile4 = 4*p4 &

// fifth sailor: divide the pile of the coconuts (pile4) into 5 parts
// (p5 each), give 1 coconut to the monkey, hide 1/5 of the coconuts,
// leaving the resulting pile (pile5) 4 parts of coconuts (4*p5)
p5::Coconuts & pile4 = 5*p5 + 1 & pile5 = 4*p5 &

// final split: divide the final pile of the coconuts (pile5) into 5 parts
// (p6 each), give 1 coconut to the monkey
p6::Coconuts & pile5 = 5*p6 + 1

```