How to resolve the algorithm Sailors, coconuts and a monkey problem step by step in the REXX programming language
How to resolve the algorithm Sailors, coconuts and a monkey problem step by step in the REXX programming language
Table of Contents
Problem Statement
Five sailors are shipwrecked on an island and collect a large pile of coconuts during the day. That night the first sailor wakes up and decides to take his first share early so tries to divide the pile of coconuts equally into five piles but finds that there is one coconut left over, so he tosses it to a monkey and then hides "his" one of the five equally sized piles of coconuts and pushes the other four piles together to form a single visible pile of coconuts again and goes to bed. To cut a long story short, each of the sailors in turn gets up once during the night and performs the same actions of dividing the coconut pile into five, finding that one coconut is left over and giving that single remainder coconut to the monkey. In the morning (after the surreptitious and separate action of each of the five sailors during the night), the remaining coconuts are divided into five equal piles for each of the sailors, whereupon it is found that the pile of coconuts divides equally amongst the sailors with no remainder. (Nothing for the monkey in the morning.)
Let's start with the solution:
Step by Step solution about How to resolve the algorithm Sailors, coconuts and a monkey problem step by step in the REXX programming language
Source code in the rexx programming language
/*REXX program solves a riddle of 5 sailors, a pile of coconuts, and a monkey. */
parse arg L H .; if L=='' then L= 5 /*L not specified? Then use default.*/
if H=='' then H= 6 /*H " " " " default.*/
/*{Tars is an old name for sailors.} */
do n=L to H /*traipse through a number of sailors. */
do $=0 while \valid(n, $) /*perform while not valid coconuts. */
end /*$*/
say 'sailors='n " coconuts="$ /*display number of sailors & coconuts.*/
end /*n*/
exit 0 /*stick a fork in it, we're all done. */
/*──────────────────────────────────────────────────────────────────────────────────────*/
valid: procedure; parse arg n,nuts /*obtain the number sailors & coconuts.*/
do k=n by -1 for n /*step through the possibilities. */
if nuts//n \== 1 then return 0 /*Not one coconut left? No solution. */
nuts=nuts - (1 + nuts % n) /*subtract number of coconuts from pile*/
end /*k*/
return (nuts \== 0) & \(nuts//n \== 0) /*see if number coconuts>0 & remainder.*/
/*REXX program solves a riddle of 5 sailors, a pile of coconuts, and a monkey. */
do n=2 to 9 /*traipse through number of sailors. */
do $=0; nuts= $ /*perform while not valid # coconuts. */
do k=n by -1 for n /*step through the possibilities. */
if nuts//n\==1 then iterate $ /*Not one coconut left? No solution.*/
nuts= nuts - (1 + nuts % n) /*subtract number of coconuts from pile*/
end /*k*/
if (nuts\==0) & \(nuts//n\==0) then leave /*is this a solution to the riddle ? */
end /*$*/
say 'sailors='n " coconuts="$ /*display number of sailors & coconuts.*/
end /*n*/ /*stick a fork in it, we're all done. */
/*REXX program solves a riddle of 5 sailors, a pile of coconuts, and a monkey. */
parse arg L H .; if L=='' then L= 2 /*L not specified? Then use default.*/
if H=='' then H= 9 /*H " " " " " */
do n=L to H /*traipse through the number of sailors*/
do $=1 until t\==0; t= total(n, $) /*perform while number coconuts not 0. */
end /*$*/
say 'sailors='n " coconuts="t ' share='$
end /*n*/
exit 0 /*stick a fork in it, we're all done. */
/*──────────────────────────────────────────────────────────────────────────────────────*/
total: procedure; parse arg n,nuts /*obtain the number sailors & coconuts.*/
nuts= nuts * n /*multiple # nuts by number of sailors.*/
nn= n - 1 /*NN is used as calculation shortcut. */
do k=0 for n /*step through the possibilities. */
if nuts//nn\==0 then return 0 /*Not one coconut left? No solution. */
nuts= nuts + 1 + nuts % nn /*bump the number coconuts to the pile.*/
end /*k*/
return nuts /*see if number coconuts>0 & remainder.*/
You may also check:How to resolve the algorithm Arithmetic/Integer step by step in the Wren programming language
You may also check:How to resolve the algorithm Dinesman's multiple-dwelling problem step by step in the MiniZinc programming language
You may also check:How to resolve the algorithm Compiler/syntax analyzer step by step in the ObjectIcon programming language
You may also check:How to resolve the algorithm Longest increasing subsequence step by step in the AutoHotkey programming language
You may also check:How to resolve the algorithm Send an unknown method call step by step in the Déjà Vu programming language