Five pirates 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 pirate awakens, suspicious that the others might try to cheat him, and decides 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 pirate 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 pirates do the same: they each take one fifth of the coconuts in the pile (after giving the extra one to the monkey) and then return to sleep.
When the pirates awaken 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 (and last) time, but this time there is no coconut left for the monkey.
How many coconuts were in the original pile?
The answer to the above puzzle is 3121. You may look ahead to the first example below to check that this is indeed the correct answer. But what if there were six pirates, or seven, or thirty? What is the correct answer in those cases? In this assignment we check how $$n$$ coconuts can be divided among $$p$$ pirates, and how many coconuts are left for the monkey, if the pirates apply the procedure as outlined in the above puzzle.
The input contains the number of pirates $$p$$ and the number of coconuts $$n$$ the pirates have initially collected, each on a separate line.
For each pirate, output a single line that is formatted as:
a nut(s) = b nut(s) for pirate#n and c nut(s) for the monkey
This indicates the number of coconuts the pirate takes as his share during the night, and how many nuts he gives to the monkey. The fragments in italic need to be filled up with the following values:
$$a$$ is the number of coconuts the pirate finds in the pile when he wakes up during the night
$$b$$ is the number of coconuts the pirate hides during the night
$$c$$ is the number of coconuts the pirate gives to the monkey during the night
$$n$$ is the sequential number of the pirate (pirates are numbered #1, #2, …)
Use the correct singular or plural form in outputting a number of coconuts:
number of coconuts | is outputted as |
---|---|
0 nut(s) | no nuts |
1 nut(s) | 1 nut |
n nut(s) with $$n \geq 2$$ | n nuts |
Finally, another line needs to be outputted that indicates how the remaining coconuts are divided among the pirates and the monkey when morning breaks. This line needs to be formatted as:
each pirate gets d nut(s) and e nut(s) for the monkey
The fragments in italic need to be filled up with the following values:
$$d$$ is the number of coconuts each pirate gets in the morning
$$e$$ is the number of coconuts the monkey gets in the morning
Again, in outputting a number of coconuts, the correct singular or plural form must be taken into account as indicated in the above table.
This example corresponds to the puzzle as given in the introduction.
Input:
5
3121
Output:
3121 nuts = 624 nuts for pirate#1 and 1 nut for the monkey
2496 nuts = 499 nuts for pirate#2 and 1 nut for the monkey
1996 nuts = 399 nuts for pirate#3 and 1 nut for the monkey
1596 nuts = 319 nuts for pirate#4 and 1 nut for the monkey
1276 nuts = 255 nuts for pirate#5 and 1 nut for the monkey
each pirate gets 204 nuts and no nuts for the monkey
Input:
4
1234
Output:
1234 nuts = 308 nuts for pirate#1 and 2 nuts for the monkey
924 nuts = 231 nuts for pirate#2 and no nuts for the monkey
693 nuts = 173 nuts for pirate#3 and 1 nut for the monkey
519 nuts = 129 nuts for pirate#4 and 3 nuts for the monkey
each pirate gets 96 nuts and 3 nuts for the monkey
Anning N (1951). Monkeys and coconuts. Mathematics Teacher 54, 560–562.
Bowden J (1936). The Problem of the Dishonest Men, the Monkeys, and the Coconuts. In Special Topics in Theoretical Arithmetic, 203–212. Lancaster Press, Lancaster, PA. 1
Gardner M (1961). The Monkey and the Coconuts. In The Second Scientific American Book of Puzzles & Diversions: A New Selection, Chapter 9, 104–111. 2
Kirchner RB (1960). The Generalized Coconut Problem. The American Mathematical Monthly 67(6), 516–519. 3
Ogilvy CS, Anderson JT (1988). Excursions in Number Theory. New York, Dover, 52-54. 4
Olds CD (1963). Continued Fractions. New York, Random House, 48-50. 5
Pappas T (1989). The Monkey and the Coconuts. The Joy of Mathematics. San Carlos, CA, 226-227. 6
Underwood RS, Moritz RE (1928). Solution to problem 3242. The American Mathematical Monthly 35(1), 47–48.
Williams (October 9, 1926). Coconuts. The Saturday Evening Post.