Monkey and Coconut Problem

Author

Eric W. Weisstein
September 5, 2004

This notebook downloaded from http://mathworld.wolfram.com/notebooks/DiophantineEquations/MonkeyandCoconutProblem.nb.

For more information, see Eric's MathWorld entry http://mathworld.wolfram.com/MonkeyandCoconutProblem.html.

©2005 Wolfram Research, Inc. except for portions noted otherwise

Code

Cocos[n_,m_]:=Cocos[n,m,m]

Cocos[n_,m_,m_]:=Module[{k},
k=Ceiling[k/.Solve[k n^(n+1)==m(n-1),k]][[1]];
k n^(n+1)-m(n-1)
]

Cocos[n_?OddQ,m_,0]:=Module[{k},
k=Ceiling[k/.Solve[(1+n k)n^n==m(n-1),k]][[1]];
(1+n k)n^n-(n-1)
]

Cocos[n_?EvenQ,m_,0]:=Module[{k},
k=Ceiling[k/.Solve[(n-1+n k)n^n==m(n-1),k]][[1]];
(n-1+n k)n^n-(n-1)
]
CocosList[n_,m_]:=CocosList[n,m,m]

CocosList[n_,m_,k_]:=Module[{t=Cocos[n,m,k],l,i},
l={{0,0,t}};
Do[
AppendTo[l,{Floor[t/n],t-n Floor[t/n],t-Floor[t/n]-(t-n Floor[t/n])}];
t-=Floor[t/n]+(t-n Floor[t/n]),
{i,n}
];
AppendTo[l,{n Floor[ l[[-1,-1]]/n],t-n Floor[ l[[-1,-1]]/n],0}]
]
EquationList[n_,m_]:=(And@@(Table[nn-Plus@@Array[a,k-1]-(k-1) m==n a[k]+m,{k,n+1}]))&&Element[Evaluate[Alternatives@@Append[Array[a,n+1],nn]],Integers]

5 men, 1 cocunut left over at each division, 1 coconut left at end

Solve[{nn==n a+m,
(n-1)a==n b+m,(n-1)b==n c+m,(n-1)c==n d+m,(n-1)d==n e+m,(n-1)e==n f+m}/.{n->5,m->1},{nn,a,b,c,d,e,f}]

Solve[{nn==n a1+m,
nn-a1-m==n a2+m,
nn-a1-a2-2m==n a3+m,
nn-a1-a2-a3-3m==n a4+m,
nn-a1-a2-a3-a4-4m==n a5+m,
 nn-a1-a2-a3-a4-a5-5m==n a+m
}/.{n->5,m->1},{nn,a1,a2,a3,a4,a5,a}]

EquationList[5,1]//ColumnForm

Reduce[EquationList[5,1],nn]

Reduce[1024nn==15625a+11529&&Element[nn|a,Integers]&&nn>0,nn]

n men, m cocunuts left over at each division, m coconuts left at end

Table[Cocos[n,1],{n,10}]

Table[Cocos[n,2],{n,10}]

Table[Cocos[n,3],{n,10}]

CocosList[5,1]

n men, m cocunuts left over at each division, no coconuts left at end

Table[Cocos[n,1,0],{n,10}]

CocosList[5,1,0]