The monkey and the coconuts
The monkey and the coconuts is a mathematical puzzle in the field of Diophantine analysis that originated in a magazine fictional short story involving five sailors and a monkey on a desert island who divide up a pile of coconuts; the problem is to find the number of coconuts in the original pile (fractional coconuts not allowed). The problem is notorious for its confounding difficulty to unsophisticated puzzle solvers, though with the proper mathematical approach, the solution is trivial. The problem has become a staple in recreational mathematics collections. General description. The problem can be  expressed as: The monkey and the coconuts is the best known representative of a class of puzzle problems requiring integer solutions structured as recursive division or fractionating of some discretely divisible quantity, with or without remainders, and a final division into some number of equal parts, possibly with a remainder. The problem is so well known that the entire class is often referred to broadly as "monkey and coconut type problems", though most are not closely related to the problem. Another example: "I have a whole number of pounds of cement, I know not how many, but after addition of  a ninth and an eleventh, it was partitioned into 3 sacks, each with a whole number of pounds. How many pounds of cement did I have?" Problems ask for either the initial or terminal quantity. Stated or implied is the smallest positive number that could be a solution. There are two unknowns in such problems, the initial number and the terminal number, but only one equation which is an algebraic reduction of an expression for the relation between them. Common to the class is the nature of the resulting equation, which is a linear Diophantine equation in two unknowns. Most  members of the class are determinate, but some are not (the monkey and the coconuts is one of the latter). Familiar algebraic methods are unavailing for solving such equations. History. The origin of the class of such problems has been attributed to the Indian mathematician Mahāvīra in chapter VI, §131, 132 of his "Ganita-sara-sangraha" (“Compendium of the Essence of Mathematics”), circa 850CE, which dealt with serial division of fruit and flowers with specified remainders. That would make progenitor problems over 1000 years old before their resurgence in the modern era. Problems involving division which invoke the Chinese remainder  theorem appeared in Chinese literature as early as the first century CE. Sun Tzu asked: Find a number which leaves the remainders 2, 3 and 2 when divided by 3, 5 and 7, respectively. Diophantus of Alexandria first studied problems requiring integer solutions in the 3rd century CE. The Euclidean algorithm for greatest common divisor which underlies the solution of such problems was discovered by the Greek geometer Euclid and published in his "Elements" in 300CE. Prof. David Singmaster, a historian of puzzles, traces a series of less plausibly related problems through the middle ages, with a few references as  far back as the Babylonian empire circa 1700BC. They involve the general theme of adding or subtracting fractions of a pile or specific numbers of discrete objects and asking how many there could have been in the beginning. The next reference to a similar problem is in Jacques Ozanam's "Récréations mathématiques et physiques", 1725. In the realm of pure mathematics, Lagrange in 1770 expounded his continued fraction theorem and applied it to solution of Diophantine equations. The first description of the problem in close to its modern wording appeared in the diaries of the mathematician and author Lewis Carroll  ("Alice in Wonderland") in 1888, involving a pile of nuts on a table serially divided by four brothers, each time with remainder of one given to a monkey, and the final division comes out even. The problem never appeared in any of the author's published works, though from other references it appears the problem was in circulation in 1888. An almost identical problem soon appeared in W.W. Rouse Ball's "Elementary Algebra", 1890. Such propinquity suggests a common source; dissemination of the problem may have occurred via Carroll's exchanges with Bartholomew Price, professor of mathematics and Carrol's  friend and tutor. Four renditions of the problem existed: two forms, one with remainders of one and another with remainders of zero but nuts discarded after division, and two endings, one where the final division has a remainder and one where it comes out even (or no nuts are discarded). The problem was mentioned in works of period mathematicians, with solutions, mostly wrong, indicating that the problem was new and unfamiliar at the time. The device of marked objects (see Blue coconuts, below) to aid in conceptualizing the division with remainders first appeared in 1912 in the work of Norman  H. Anning involving a bin of apples divided by three men. The problem became notorious when American novelist and short story writer Ben Ames Williams modified an older problem and included it in a story, "Coconuts", in the October 9, 1926 issue of the "Saturday Evening Post". Here is how the problem was stated by Williams (condensed and paraphrased): Williams had not included an answer in the story. The magazine was inundated by more than 2,000 letters pleading for an answer to the problem. The Post editor, Horace Lorimer, famously fired off a telegram to Williams saying: "FOR THE LOVE  OF MIKE, HOW MANY COCONUTS? HELL POPPING AROUND HERE". Williams continued to get letters asking for a solution or proposing new ones for the next twenty years. Martin Gardner featured the problem in his April 1958 Mathematical Games column in "Scientific American". He stated that Williams had modified an older problem to make it more confounding. In the older version there is a coconut for the monkey on the final division; in Williams's version the final division in the morning comes out even. But the available historical evidence doesn't indicate which versions Williams had access to. Gardner once  told his son Jim that it was his favorite problem, so much so that he later chose to make it the first chapter of his "best of columns" collection, "The Colossal Book of Mathematics". He said that the Monkey and the Coconuts is "probably the most worked on and least often solved" Diophantine puzzle. Since that time the Williams version of the problem has become a staple of recreational mathematics. The original story containing the problem was reprinted in full in Clifton Fadiman's 1962 anthology "The Mathematical Magpie", a book that the Mathematical Association of America recommends for acquisition  by undergraduate mathematics libraries. Numerous variants which vary the number of sailors, monkeys, or coconuts have appeared in the literature. Solutions. Numerous solutions starting as early as 1928 have been published both for the original problem and Williams modification. Clever and succinct solutions using modulo congruences, sieves, and alternate number bases have been devised based partly or mostly on the recursive definition of the problem, a structure that won't be applicable in the general case. The smallest positive solutions to both versions are sufficiently large that trial and error is very likely to be fruitless. An ingenious concept of  negative coconuts was introduced that fortuitously solves the original problem. Formalistic solutions are based on Euclid's algorithm applied to the Diophantine coefficients. Finally, the "calculus of finite differences" yields a parameterized general solution for any number of sailors and all multiples of coconuts that could have been on the original pile. In modern times, a computer brute force search over the positive integers quickly yields the solution. Before entering upon a solution to the problem, a couple of things may be noted. If there were no remainders, given there are 6 divisions of 5, 5=15,625 coconuts must be in  the pile; on the 6th and last division, each sailor receives 1024 coconuts. No smaller positive number will result in all 6 divisions coming out even. That means that in the problem as stated, any multiple of 15,625 may be added to the pile, and it will satisfy the problem conditions. That also means that the number of coconuts in the original pile is smaller than 15,625, else subtracting 15,625 will yield a smaller solution. But the number in the original pile isn't trivially small, like 5 or 10 (that's why this is a hard problem) - it may  be in the hundreds or thousands. Unlike trial and error in the case of guessing a polynomial root, trial and error for a Diophantine root will not result in any obvious convergence. There's no simple way of estimating what the solution will be. The original version. A summary analysis of both the original problem and Williams's version was presented by Martin Gardner when he featured the problem in his Mathematical Games column. Gardner begins by solving the original problem because it is less confounding than the Williams variation. Let "N" be the size of the original pile and  F be the number of coconuts received by each sailor after the final division into 5 equal shares in the morning. Then the number of coconuts left before the morning division is F5+1. If that quantity is designated "n", the number remaining before the last sailor's division is: reversing the procedure of the sailor during the night. But each sailor followed the same procedure; there are thus defined a recursive series of 5 such formula_2s (replacing formula_3 with formula_2 and generating a new formula_2), the fifth and last of which is "N" itself, the number of coconuts  before the division by the first sailor. Successively substituting the formula_2s and formula_3 yields: which reduces to the Diophantine equation: By a fundamental theorem, this equation has a solution if and only if 11529 is a multiple of the Greatest Common Divisor of 1024 and 15625. 1024=4 and 15625=5, so their GCD is 1, and 11529 is a multiple of 1 so the equation is solvable. Gardner points out that this equation is much too difficult to solve by trial and error. Moreover, it has an infinite number of solutions. But Gardner was mistaken about the difficulty. In fact, if  (N, F) is a solution then so is "(N + 15625 t, F + 1024 t)" for any integer "t". This means that the equation also has solutions in negative integers. Trying out a few small negative numbers it turns out N = -4 and F = -1 is a solution. This involves the absurd notion of negative coconuts; so we add 15625 to -4 and add 1024 to -1 to get the smallest positive solution "(15621, 1023)". Williams version. The negative coconuts approach doesn't apply to the Williams version, at least not for any reasonably small |"N"|, so  a more systematic approach is needed. Using a sieve. The search space can be reduced by a series of increasingly larger factors by observing the structure of the problem so that a bit of trial and error finds the solution. The search space is much smaller if one starts with the number of coconuts received by each man in the morning division, because that number is much smaller than the number in the original pile. If "F" is the number of coconuts each sailor receives in the final division in the morning, the pile in the morning is 5"F", which  must also be divisible by 4, since the last sailor in the night combined 4 piles for the morning division. So the morning pile, call the number "n", is a multiple of 20. The pile before the last sailor woke up must have been 5/4("n")+1. If only one sailor woke up in the night, then 5/4(20)+1 = 26 works for the minimum number of coconuts in the original pile. But if two sailors woke up, 26 is not divisible by 4, so the morning pile must be some multiple of 20 that yields a pile divisible by 4 before the  last sailor wakes up. It so happens that 3*20=60 works for two sailors: applying the recursion formula for "n" twice yields 96 as the smallest number of coconuts in the original pile. 96 is divisible by 4 once more, so for 3 sailors awakening, the pile could have been 121 coconuts. But 121 isn't divisible by 4, so for 4 sailors awakening, one needs to make another leap. At this point, the analogy becomes obtuse, because in order to accommodate 4 sailors awakening, the morning pile must be some multiple of 60: if one is persistent, it may  be discovered that 17*60=1020 does the trick and the minimum number in the original pile would be 2496. A last iteration on 2496 for 5 sailors awakening, i.e. 5/4(2496)+1 brings the original pile to 3121 coconuts. Blue coconuts. Another device predating the problem, is to use extra or marked objects, in this case blue coconuts, to clarify the division process. Suppose that the first sailor before the division, adds four blue coconuts to the pile to guarantee division by 5 (since we know even if he doesn't, that there's going to be a remainder of 1, so  adding 4 makes the pile divisible by 5). He divides the pile, takes the fifth with an extra (non-blue) coconut which he tosses to the monkey, hides his share, then puts the rest back together, putting the 4 blue coconuts to the side. Each sailor does the same. After the fifth sailor (or after the division in the morning if there's a remainder there), the blue coconuts are left on the side, belonging to no one. Since the original pile is divided 5 times (or 6, if there's a remainder in the morning), including the blue coconuts  it must have been 5 (5) coconuts. That makes the pile 3125-4=3121 (15625-4=15621) coconuts. The blue coconuts may be considered to be "virtual" coconuts which play no role in the problem, only serving to clarify the divisibility. Base 5 numbering. A simple and obvious solution appears when the divisions and subtractions are performed in base 5. Consider the subtraction, when the first sailor takes his share (and the monkey's). Let n,n... represent the digits of N, the number of coconuts in the original pile, and s,s... represent the digits of the sailor's share S , both base 5. After the monkey's share, the least significant digit of N must now be 0; after the subtraction, the least significant digit of N' left by the first sailor must be 1, hence the following (the actual number of digits in N as well as S is unknown, but they're irrelevant just now): The digit subtracted from 0 base 5 to yield 1 is 4, so s=4. But since S is (N-1)/5, and dividing by 5 is just shifting the number right one position, n=s=4. So now the subtraction looks like: Since the next  sailor is going to do the same thing on N', the least significant digit of N' becomes 0 after tossing one to the monkey, and the LSD of S' must be 4 for the same reason; the next digit of N' must also be 4. So now it looks like: Borrowing 1 from n (which is now 4) leaves 3, so s must be 4, and therefore n as well. So now it looks like: But the same reasoning again applies to N' as applied to N, so the next digit of N' is 4, so s and n are  also 4, etc. There are 5 divisions; the first four must leave an odd number base 5 in the pile for the next division, but the last division must leave an even number base 5 so the morning division will come out even (in 5s). So there are four 4s in N following a LSD of 1: N=44441=3121 A numerical approach. A straightforward numeric analysis goes like this: If N is the initial number, each of 5 sailors transitions the original pile thus: Repeating this transition 5 times gives the number left in the morning: Since that number must be  an integer and 1024 is relatively prime to 3125, N+4 must be a multiple of 3125. The smallest such multiple is 31251, so N = 3125 – 4 = 3121; the number left in the morning comes to 1020, which is evenly divisible by 5 as required. Modulo congruence. A simple succinct solution can be obtained by directly utilizing the recursive structure of the problem: There were five divisions of the coconuts into fifths, each time with one left over (putting aside the final division in the morning). The pile remaining after each division must contain an integral number of coconuts . If there were only one such division, then it is readily apparent that 51+1=6 is a solution. In fact any multiple of five plus one is a solution, so a possible general formula is 5"k" - 4, since a multiple of 5 plus 1 is also a multiple of 5 minus 4. So 11, 16, etc also work for one division. If two divisions are done, a multiple of 55=25 rather than 5 must be used, because 25 can be divided by 5 twice. So the number of coconuts that could be in the pile is "k"25 - 4. "k"=1  yielding 21 is the smallest positive number that can be successively divided by 5 twice with remainder 1. If there are 5 divisions, then multiples of 5=3125 are required; the smallest such number is 3125 - 4 = 3121. After 5 divisions, there are 1020 coconuts left over, a number divisible by 5 as required by the problem. In fact, after "n" divisions, it can be proven that the remaining pile is divisible by "n", a property made convenient use of by the creator of the problem. A formal way of stating the above argument is: The original pile of coconuts  will be divided by 5 a total of 5 times with a remainder of 1, not considering the last division in the morning. Let N = number of coconuts in the original pile. Each division must leave the number of nuts in the same congruence class (mod 5). So, So if we began in modulo class –4 nuts then we will remain in modulo class –4. Since ultimately we have to divide the pile 5 times or 5^5, the original pile was 5^5 – 4 = 3121 coconuts. The remainder of 1020 coconuts conveniently divides evenly by  5 in the morning. This solution essentially reverses how the problem was (probably) constructed. The Diophantine equation and forms of solution. The equivalent Diophantine equation for this version is: where "N" is the original number of coconuts, and "F" is the number received by each sailor on the final division in the morning. This is only trivially different than the equation above for the predecessor problem, and solvability is guaranteed by the same reasoning. Reordering, This Diophantine equation has a solution which follows directly from the Euclidean algorithm; in fact, it has infinitely many periodic solutions positive and negative. If  (x, y) is a solution of 1024x–15625y=1, then N=x8404, F=y8404 is a solution of (2), which means any solution must have the form where formula_16 is an arbitrary parameter that can have any integral value. A reductionist approach. One can take both sides of (1) above modulo 1024, so Another way of thinking about it is that in order for formula_3 to be an integer, the RHS of the equation must be an integral multiple of 1024; that property will be unaltered by factoring out as many multiples of 1024 as possible from the RHS. Reducing  both sides by multiples of 1024, subtracting, factoring, The RHS must still be a multiple of 1024; since 53 is relatively prime to 1024, 5"F"+4 must be a multiple of 1024. The smallest such multiple is 11024, so 5"F"+4=1024 and F=204. Substituting into (1) Euclidean algorithm. The Euclidean algorithm is quite tedious but a general methodology for solving rational equations ax+by=c requiring integral answers. From (2) above, it is evident that 1024 (2) and 15625 (5) are relatively prime and therefore their GCD is 1, but we need the reduction equations for back substitution to obtain "N" and  F in terms of these two quantities: First, obtain successive remainders until GCD remains: 15625 = 15·1024 + 265 (a) 1024 = 3·265 + 229 (b) 265 = 1·229 + 36 (c) 229 = 6·36 + 13 (d) 36 = 2·13 + 10 (e) 13 = 1·10 + 3 (f) 10 = 3·3 + 1 (g) (remainder 1 is GCD of 15625 and 1024) 1 = 10 – 3(13–1·10) = 4·10 – 3·13 (reorder (g), substitute for 3 from (f) and combine) 1 = 4·(36 – 2·13) – 3·13 = 4·36 – 11·13 (substitute for 10 from (e) and combine) 1 = 4·36 – 11·(229 – 6·36 ) = –11·229 + 70*36 (substitute for 13 from (d) and combine) 1 = –11·229 + 70·(265 – 1·229) = –81·229 + 70·265 (substitute for 36 from (c) and combine) 1 = –81·(1024 – 3·265) + 70·265 = –81·1024 + 313·265 (substitute for 229 from (b) and combine) 1 = –81·1024 + 313·(15625 – 15·1024) = 313·15625 – 4776·1024 (substitute for 265 from (a) and combine) So the pair (N,F) = (-4776·8404, -313*8404); the smallest formula_16 (see (3) in the previous subsection) that will make both N and F positive is 2569, so: Continued fraction. Alternately, one may use a continued fraction , whose construction is based on the Euclidean algorithm. The continued fraction for (0.065536 exactly) is [;15,3,1,6,2,1,]; its convergent terminated after the repetend is , giving us x=–4776 and y=313. The least value of "t" for which both N and F are non-negative is 2569, so This is the smallest positive number that satisfies the conditions of the problem. A generalized solution. When the number of sailors is a parameter, let it be formula_27, rather than a computational value, careful algebraic reduction of the relation between the number of coconuts in the original pile and the number allotted to each sailor  in the morning yields an analogous Diophantine relation whose coefficients are expressions in formula_27. The first step is to obtain an algebraic expansion of the recurrence relation corresponding to each sailor's transformation of the pile, formula_29 being the number left by the sailor: where formula_31, the number originally gathered, and formula_32 the number left in the morning. Expanding the recurrence by substituting formula_29 for formula_34 formula_27 times yields: Factoring the latter term, The power series polynomial in brackets of the form formula_38 sums to formula_39 so, which simplifies to: But formula_42 is the number left in the morning which  is a multiple of formula_27 (i.e. formula_44, the number allotted to each sailor in the morning): Solving for formula_46(=formula_47), The equation is a linear Diophantine equation in two variables, formula_47 and formula_44. formula_27 is a parameter that can be any integer. The nature of the equation and the method of its solution do not depend on formula_27. Number theoretic considerations now apply. For formula_47 to be an integer, it is sufficient that formula_54 be an integer, so let it be formula_55: The equation must be transformed into the form formula_57 whose solutions are formulaic. Hence: Because formula_27 and formula_61 are  relatively prime, there exist integer solutions formula_62 by Bézout's identity. This equation can be restated as: But ("m"–1) is a polynomial "Z'm"–1 if "m" is odd and "Z'm"+1 if "m" is even, where "Z" is a polynomial with monomial basis in "m". Therefore "r"=1 if "m" is odd and "r"=–1 if "m" is even is a solution. Bézout's identity gives the periodic solution formula_64, so substituting for formula_55 in the Diophantine equation and rearranging: where formula_67 for formula_27 odd and formula_69 for formula_27 even and formula_71 is any integer. For a given formula_27, the smallest positive formula_71 will  be chosen such that formula_47 satisfies the constraints of the problem statement. In the William's version of the problem, formula_27 is 5 sailors, so formula_76 is 1, and formula_71 may be taken to be zero to obtain the lowest positive answer, so "N" = 1 5 – 4 = 3121 for the number of coconuts in the original pile. (It may be noted that the next sequential solution of the equation for "k"=–1, is –12504, so trial and error around zero will not solve the Williams version of the problem, unlike the original version whose equation, fortuitously, had a small  magnitude negative solution). Here is a table of the positive solutions formula_47 for the first few formula_27 (formula_71 is any non-negative integer): Other Variants and general solutions. Other variants, including the putative predecessor problem, have related general solutions for an arbitrary number of sailors. When the morning division also has a remainder of one, the solution is: For formula_82 and formula_83 this yields 15,621 as the smallest positive number of coconuts for the pre-William's version of the problem. In some earlier alternate forms of the problem, the divisions came out even, and nuts (or items) were allocated  from the remaining pile "after" division. In these forms, the recursion relation is: The alternate form also had two endings, when the morning division comes out even, and when there is one nut left over for the monkey. When the morning division comes out even, the general solution reduces via a similar derivation to: For example, when formula_86 and formula_83, the original pile has 1020 coconuts, and after four successive even divisions in the night with a coconut allocated to the monkey after each division, there are 80 coconuts left over in the morning, so the final division comes out  even with no coconut left over. When the morning division results in a nut left over, the general solution is: where formula_69 if formula_27 is odd, and formula_67 if formula_27 is even. For example, when formula_93, formula_69 and formula_83, the original pile has 51 coconuts, and after three successive divisions in the night with a coconut allocated to the monkey after each division, there are 13 coconuts left over in the morning, so the final division has a coconut left over for the monkey. Other post-Williams variants which specify different remainders including positive ones (i.e. the monkey adds coconuts  to the pile), have been treated in the literature. The solution is: where formula_67 for formula_27 odd and formula_69 for formula_27 even, formula_101 is the remainder after each division (or number of monkeys) and formula_71 is any integer (formula_101 is negative if the monkeys add coconuts to the pile). Other variants in which the number of men or the remainders vary between divisions, are generally outside the class of problems associated with the monkey and the coconuts, though these similarly reduce to linear Diophantine equations in two variables. Their solutions yield to the same techniques and present no new difficulties