How to resolve the algorithm Sailors, coconuts and a monkey problem step by step in the AutoHotkey programming language
How to resolve the algorithm Sailors, coconuts and a monkey problem step by step in the AutoHotkey 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 AutoHotkey programming language
Source code in the autohotkey programming language
loop, 2
{
sailor := A_Index+4
while !result := Coco(sailor, A_Index)
continue
; format output
remain := result["Coconuts"]
output := sailor " Sailors, Number of coconuts = " result["Coconuts"] "`n"
loop % sailor {
x := result["Sailor_" A_Index]
output .= "Monkey gets 1, Sailor# " A_Index " hides (" remain "-1)/" sailor " = " x ", remainder = " (remain -= x+1) "`n"
}
output .= "Remainder = " result["Remaining"] "/" sailor " = " floor(result["Remaining"] / sailor)
MsgBox % output
}
return
Coco(sailor, coconut){
result := [], result["Coconuts"] := coconut
loop % sailor {
if (Mod(coconut, sailor) <> 1)
return
result["Sailor_" A_Index] := Floor(coconut/sailor)
coconut -= Floor(coconut/sailor) + 1
}
if Mod(coconut, sailor) || !coconut
return
result["Remaining"] := coconut
return result
}
You may also check:How to resolve the algorithm Strip comments from a string step by step in the PL/I programming language
You may also check:How to resolve the algorithm Stack step by step in the REXX programming language
You may also check:How to resolve the algorithm Run-length encoding step by step in the Common Lisp programming language
You may also check:How to resolve the algorithm Taxicab numbers step by step in the Delphi programming language
You may also check:How to resolve the algorithm Concurrent computing step by step in the Logtalk programming language