How to resolve the algorithm Sailors, coconuts and a monkey problem step by step in the Elixir programming language
How to resolve the algorithm Sailors, coconuts and a monkey problem step by step in the Elixir 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 Elixir programming language
Source code in the elixir programming language
defmodule RC do
def valid?(sailor, nuts), do: valid?(sailor, nuts, sailor)
def valid?(sailor, nuts, 0), do: nuts > 0 and rem(nuts,sailor) == 0
def valid?(sailor, nuts, _) when rem(nuts,sailor)!=1, do: false
def valid?(sailor, nuts, i) do
valid?(sailor, nuts - div(nuts,sailor) - 1, i-1)
end
end
Enum.each([5,6], fn sailor ->
nuts = Enum.find(Stream.iterate(sailor, &(&1+1)), fn n -> RC.valid?(sailor, n) end)
IO.puts "\n#{sailor} sailors => #{nuts} coconuts"
Enum.reduce(0..sailor, nuts, fn _,n ->
{d, r} = {div(n,sailor), rem(n,sailor)}
IO.puts " #{inspect [n, d, r]}"
n - 1 - d
end)
end)
defmodule Sailor do
def coconuts(sailor), do: coconuts(sailor, sailor)
defp coconuts(sailor, nuts) do
if n = do_coconuts(sailor, nuts, sailor), do: n, else: coconuts(sailor, nuts+sailor)
end
defp do_coconuts(_sailor, nuts, 0), do: nuts
defp do_coconuts(sailor, nuts, _) when rem(nuts, sailor-1) != 0, do: nil
defp do_coconuts(sailor, nuts, i) do
do_coconuts(sailor, nuts + div(nuts, sailor-1) + 1, i-1)
end
end
Enum.each(2..9, fn sailor ->
IO.puts "#{sailor}: #{Sailor.coconuts(sailor)}"
end)
You may also check:How to resolve the algorithm Narcissistic decimal number step by step in the Prolog programming language
You may also check:How to resolve the algorithm Bulls and cows step by step in the Hy programming language
You may also check:How to resolve the algorithm Loops/Infinite step by step in the C programming language
You may also check:How to resolve the algorithm Barnsley fern step by step in the Standard ML programming language
You may also check:How to resolve the algorithm Walk a directory/Non-recursively step by step in the Tcl programming language