require "table2" local coconuts = 11 for ns = 2, 9 do local hidden = table.rep(ns, 0) coconuts = (coconuts // ns) * ns + 1 while true do local nc = coconuts local outer = false for s = 1, ns do if nc % ns == 1 then hidden[s] = nc // ns nc -= hidden[s] + 1 if s == ns and nc % ns == 0 then print($"{ns} sailors require a minimum of {coconuts} coconuts") for t = 1, ns do print($"\tSailor {t} hides {hidden[t]}") end print($"\tThe monkey gets {ns}") print($"\tFinally, each sailor takes {nc // ns}\n") outer = true break end else break end end if outer then break end coconuts += ns end end