Sailors, coconuts and a monkey problem: Difference between revisions

m
 
(6 intermediate revisions by 4 users not shown)
Line 32:
{{trans|Python}}
 
<langsyntaxhighlight lang="11l">F monkey_coconuts(sailors = 5)
V nuts = sailors
L
Line 50:
V (nuts, wake_stats) = monkey_coconuts(sailors)
print("\nFor #. sailors the initial nut count is #.".format(sailors, nuts))
print("On each waking, the nut count, portion taken, and monkeys share are:\n "wake_stats.map(ws -> String(ws)).join(",\n "))</langsyntaxhighlight>
 
{{out}}
Line 76:
 
=={{header|AutoHotkey}}==
<langsyntaxhighlight AutoHotkeylang="autohotkey">loop, 2
{
sailor := A_Index+4
Line 105:
result["Remaining"] := coconut
return result
}</langsyntaxhighlight>
{{out}}
<pre>---------------------------
Line 127:
 
=={{header|AWK}}==
<syntaxhighlight lang="awk">
<lang AWK>
# syntax: GAWK -f SAILORS_COCONUTS_AND_A_MONKEY_PROBLEM.AWK
# converted from LUA
Line 151:
return((nuts != 0) && (nuts % n == 0))
}
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 167:
This script implements a solution in the coconuts function for a number of sailors > 1 and a number of monkeys between 1 and sailors-1. It also executes the coconuts function for some values of sailors/monkeys.
 
<langsyntaxhighlight Bclang="bc">define coconuts(sailors, monkeys) {
print "coconuts(", sailors, ", ", monkeys, ") = "
if (sailors < 2 || monkeys < 1 || sailors <= monkeys) {
Line 189:
coconuts(5, 4)
coconuts(6, 1)
coconuts(101, 1)</langsyntaxhighlight>
 
{{out}}
Line 214:
This is a translation of the second C solution. The output lists the number of sailors, the size of the original pile, and the final share each sailor receives the following morning.
 
<langsyntaxhighlight lang="befunge">>2+:01p9>`#@_00v
nvg10*g10:+>#1$<
#>\:01g1-%#^_:0v
-|:-1\+1<+/-1g1<
1>$01g.">-",,48v
^g10,+55<.,9.,*<</langsyntaxhighlight>
 
{{out}}
Line 233:
=={{header|Bracmat}}==
{{trans|Tcl}} (Though without the <code>assert</code> procedure.)
<langsyntaxhighlight lang="bracmat">( ( divmod
= a b
. !arg:(?a.?b)&(div$(!a.!b).mod$(!a.!b))
Line 288:
& minnuts$!n
)
)</langsyntaxhighlight>
 
Output:
Line 310:
 
=={{header|C}}==
<langsyntaxhighlight lang="c">#include <stdio.h>
int valid(int n, int nuts)
Line 328:
}
return 0;
}</langsyntaxhighlight>
{{out}}
<pre>2: 11
Line 342:
But it's faster to search backwards: if everyone receives some coconuts,
see if we can backtrack to the original pile:
<langsyntaxhighlight lang="c">#include <stdio.h>
 
// calculates if everyone got some nuts in the end, what was the original pile
Line 364:
}
return 0;
}</langsyntaxhighlight>
{{out}}
sailers: original pile, final share
Line 380:
=={{header|C sharp|C#}}==
{{trans|Java}}
<langsyntaxhighlight Csharplang="csharp">class Test
{
static bool valid(int n, int nuts)
Line 405:
}
}
}</langsyntaxhighlight>
 
<pre>2: 11
Line 418:
=={{header|C++}}==
{{trans|C#}}
<langsyntaxhighlight lang="cpp">#include <iostream>
 
bool valid(int n, int nuts) {
Line 440:
 
return 0;
}</langsyntaxhighlight>
{{out}}
<pre>2: 11
Line 454:
A rather non-Clojure-like solution:
 
<langsyntaxhighlight lang="clojure">(defn solves-for? [sailors initial-coconut-count]
(with-local-vars [coconuts initial-coconut-count, hidings 0]
(while (and (> @coconuts sailors) (= (mod @coconuts sailors) 1)
Line 472:
(str "\tIn the morning, each sailor gets another " (/ @coconuts sailors) " coconuts."))
(println "\tThe monkey gets no more.\n"))))
</syntaxhighlight>
</lang>
 
{{Out}}
Line 496:
=={{header|D}}==
{{trans|Kotlin}}
<syntaxhighlight lang="d">
<lang D>
import std.stdio;
 
Line 529:
}
}
</syntaxhighlight>
</lang>
 
{{out}}
Line 613:
{{trans|Ruby}}
===Brute Force===
<langsyntaxhighlight lang="elixir">defmodule RC do
def valid?(sailor, nuts), do: valid?(sailor, nuts, sailor)
Line 631:
n - 1 - d
end)
end)</langsyntaxhighlight>
 
{{out}}
Line 654:
 
===Faster version===
<langsyntaxhighlight lang="elixir">defmodule Sailor do
def coconuts(sailor), do: coconuts(sailor, sailor)
defp coconuts(sailor, nuts) do
Line 669:
Enum.each(2..9, fn sailor ->
IO.puts "#{sailor}: #{Sailor.coconuts(sailor)}"
end)</langsyntaxhighlight>
 
{{out}}
Line 685:
=={{header|Forth}}==
{{trans|uBasic/4tH}}
<syntaxhighlight lang="text">: total
over * over 1- rot 0 ?do
over over mod if dup xor swap leave else over over / 1+ rot + swap then
Line 697:
;
 
9 sailors</langsyntaxhighlight>
{{out}}
<pre>2: 11 1
Line 711:
=={{header|FreeBASIC}}==
{{trans|Yabasic}}
<langsyntaxhighlight lang="freebasic">
Dim As Integer cocos = 11
 
Line 740:
Next ns
Sleep
</syntaxhighlight>
</lang>
 
 
=={{header|Go}}==
{{trans|Kotlin}}
<langsyntaxhighlight lang="go">package main
 
import "fmt"
Line 777:
}
}
}</langsyntaxhighlight>
 
{{out}}
Line 788:
This program works by applying a function to increasing multiples of the number of sailors. The function takes a potential final number of coconuts (at the time the sailors awaken) and works backwards to get to the initial number of coconuts. At every step, it will abort the computation if the current number of coconuts can't arise as a result of splitting the previous pile.
 
<langsyntaxhighlight lang="haskell">import Control.Monad ((>=>))
import Data.Maybe (mapMaybe)
import System.Environment (getArgs)
Line 812:
s:_ -> read s
a = head . mapMaybe (tryFor n) $ [n,2 * n ..]
print a</langsyntaxhighlight>
Examples:
<pre>
Line 827:
Here, we assume an answer which is less than 10000, and try each possibility, constraining ourselves to the list of answers which are valid. (As it happens, there's only one of those.)
 
<langsyntaxhighlight Jlang="j"> I.(=<.)%&5 verb def'4*(y-1)%5'^:5 i.10000
3121</langsyntaxhighlight>
 
These sailors must count coconuts extremely quickly.
Line 834:
When we do this with six sailors, it turns out that we have to assume a larger initial value:
 
<langsyntaxhighlight Jlang="j"> I.(=<.)%&6 verb def'5*(y-1)%6'^:6 i.1000000
233275 513211 793147</langsyntaxhighlight>
 
If it were not obvious which of the answers here was the minimum value we could additionally pick the smallest value from the list. But that would require typing two extra characters (for example, replace I. with i.&1), and most people already have so much trouble reading J that the extra code would surely be too much.
Line 841:
=={{header|Java}}==
{{trans|C}}
<langsyntaxhighlight lang="java">public class Test {
 
static boolean valid(int n, int nuts) {
Line 858:
}
}
}</langsyntaxhighlight>
<pre>2: 11
3: 25
Line 875:
( As in the recursive Python example )
 
<langsyntaxhighlight JavaScriptlang="javascript">(function () {
 
// wakeSplit :: Int -> Int -> Int -> Int
Line 897:
return intNuts;
});
})();</langsyntaxhighlight>
 
{{out}}
 
<syntaxhighlight lang JavaScript="javascript">[3121, 233275, 823537]</langsyntaxhighlight>
 
===ES6===
<langsyntaxhighlight lang="javascript">(() => {
"use strict";
 
Line 964:
// MAIN ---
return main();
})();</langsyntaxhighlight>
 
{{Out}}
<syntaxhighlight lang ="javascript">[3121, 233275, 823537]</langsyntaxhighlight>
 
=={{header|jq}}==
Line 975:
 
If your jq does not have "until" defined, here is its definition:
<langsyntaxhighlight lang="jq">def until(cond; next): def _until: if cond then . else (next|_until) end; _until;</langsyntaxhighlight>
 
===Simulation===
<langsyntaxhighlight lang="jq"># If n (the input) is an admissible number of coconuts with respect to
# the night-time squirreling away of the coconuts by "sailors" sailors, then give 1 to the
# monkey, let one sailor squirrel away (1/sailors) coconuts, and yield the remaining number;
Line 998:
 
# Test whether the input is a valid number of coconuts with respect to the story:
def valid(sailors): nighttime(sailors) | morning(sailors);</langsyntaxhighlight>
'''Five sailors''':
Find the minimum number of coconuts if there are 5 sailors --
start at 1 as there must be at least one to give to the monkey during the night:
<langsyntaxhighlight lang="jq">1 | until( valid(5); . + 1)</langsyntaxhighlight>
{{out}}
3121
Line 1,009:
Find the minimum number of coconuts if there are 6 sailors --
start at 1 as there must be at least one to give to the monkey during the night:
<langsyntaxhighlight lang="jq">1 | until( valid(6); . + 1)</langsyntaxhighlight>
{{out}}
233275
 
===Working backwards===
<langsyntaxhighlight lang="jq"># If n (the input) is the number of coconuts remaining after
# the surreptitious squirreling away by one sailor,
# then emit the number of coconuts which that sailor originally
Line 1,036:
" and each sailor finally ended up with \(.[0])." ;
range(2;9) | solve</langsyntaxhighlight>
{{out}}
<langsyntaxhighlight lang="sh">With 2 sailors, there were originally 3 coconuts, and each sailor finally ended up with 0.
With 3 sailors, there were originally 25 coconuts, and each sailor finally ended up with 2.
With 4 sailors, there were originally 765 coconuts, and each sailor finally ended up with 60.
Line 1,044:
With 6 sailors, there were originally 233275 coconuts, and each sailor finally ended up with 13020.
With 7 sailors, there were originally 823537 coconuts, and each sailor finally ended up with 39990.
With 8 sailors, there were originally 117440505 coconuts, and each sailor finally ended up with 5044200.</langsyntaxhighlight>
 
=={{header|Julia}}==
{{trans|C#}}
<langsyntaxhighlight lang="julia">
function validnutsforsailors(sailors, finalpile)
for i in sailors:-1:1
Line 1,071:
 
runsim()
</syntaxhighlight>
</lang>
{{output}}
<pre>
Line 1,086:
 
=={{header|Kotlin}}==
<langsyntaxhighlight lang="scala">// version 1.1.2
 
fun main(args: Array<String>) {
Line 1,112:
}
}
}</langsyntaxhighlight>
 
{{out}}
Line 1,195:
=={{header|Lua}}==
{{trans|C}}
<langsyntaxhighlight lang="lua">function valid(n,nuts)
local k = n
local i = 0
Line 1,214:
end
print(n..": "..x)
end</langsyntaxhighlight>
{{out}}
<pre>2: 11
Line 1,226:
 
=={{header|Mathematica}} / {{header|Wolfram Language}}==
<langsyntaxhighlight Mathematicalang="mathematica">ClearAll[SequenceOk]
SequenceOk[n_, k_] := Module[{m = n, q, r, valid = True},
Do[
Line 1,249:
i = 1;
While[! SequenceOk[i, 6], i++]
i</langsyntaxhighlight>
{{out}}
<pre>3121
Line 1,256:
 
=={{header|Modula-2}}==
<langsyntaxhighlight lang="modula2">MODULE Coconuts;
FROM FormatString IMPORT FormatString;
FROM Terminal IMPORT WriteString,WriteLn,ReadChar;
Line 1,308:
 
ReadChar
END Coconuts.</langsyntaxhighlight>
 
=={{header|Nim}}==
{{trans|Kotlin}}
<langsyntaxhighlight Nimlang="nim">import strformat
 
var coconuts = 11
Line 1,335:
else:
break # Failed. Continue search with more coconuts.
inc coconuts, ns</langsyntaxhighlight>
 
{{out}}
Line 1,416:
=={{header|Objeck}}==
{{trans|C}}
<langsyntaxhighlight lang="objeck">
class Program {
function : Total(n : Int, nuts : Int) ~ Int {
Line 1,440:
}
}
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 1,456:
Use of <code>bigint</code> required or program silently fails for number of sailors > 13
{{trans|Raku}}
<langsyntaxhighlight lang="perl">use bigint;
 
for $sailors (1..15) { check( $sailors, coconuts( 0+$sailors ) ) }
Line 1,492:
if ($sailors % 2 == 0 ) { ($sailors ** $sailors - 1) * ($sailors - 1) }
else { $sailors ** $sailors - $sailors + 1 }
}</langsyntaxhighlight>
{{out}}
<pre style="height:60ex;overflow:scroll;">
Line 1,678:
The morning pile must be a multiple of sailors, so this only tries multiples of sailors! Needed an ugly kludge for solve(1),
the limit of 1 billion suffices for solve(9), above that gets run-time type check errors as capacity of ints are blown anyway.
<!--<langsyntaxhighlight Phixlang="phix">(phixonline)-->
<span style="color: #008080;">procedure</span> <span style="color: #000000;">solve</span><span style="color: #0000FF;">(</span><span style="color: #004080;">integer</span> <span style="color: #000000;">sailors</span><span style="color: #0000FF;">)</span>
<span style="color: #004080;">integer</span> <span style="color: #000000;">m</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">sm1</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">sailors</span><span style="color: #0000FF;">-</span><span style="color: #000000;">1</span>
Line 1,708:
<span style="color: #000000;">solve</span><span style="color: #0000FF;">(</span><span style="color: #000000;">5</span><span style="color: #0000FF;">)</span>
<span style="color: #000000;">solve</span><span style="color: #0000FF;">(</span><span style="color: #000000;">6</span><span style="color: #0000FF;">)</span>
<!--</langsyntaxhighlight>-->
{{out}}
<pre>
Line 1,729:
 
=={{header|Picat}}==
<langsyntaxhighlight Picatlang="picat">main ?=>
between(2,9,N), % N: number of sailors
once s(N),
Line 1,738:
next_candidate(N+1,N,C), % C: original number of coconuts
divide(N,N,C,Cr), % Cr: remainder
Cr > 0,
Cr mod N == 0,
printf("%d: original = %d, remainder = %d, final share = %d\n",N,C,Cr,Cr div N).
 
Line 1,745 ⟶ 1,743:
next_candidate(From,Step,X) => next_candidate(From+Step,Step,X).
 
divide(N,0,C,Cr) => C > 0, C mod N == 0, Cr = C.
divide(N,I,C,Cr) =>
(C-1) mod N == 0,
Line 1,751 ⟶ 1,749:
C1 = Q*(N-1),
divide(N,I-1,C1,Cr).
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 1,767 ⟶ 1,765:
You may want to read [http://paddy3118.blogspot.co.uk/2015/05/solving-monkey-and-coconuts-problem.html Solving the Monkey and coconuts problem] to get more background on the evolution of the Python code.
===Python: Procedural===
<langsyntaxhighlight lang="python">def monkey_coconuts(sailors=5):
"Parameterised the number of sailors using an inner loop including the last mornings case"
nuts = sailors
Line 1,788 ⟶ 1,786:
print("\nFor %i sailors the initial nut count is %i" % (sailors, nuts))
print("On each waking, the nut count, portion taken, and monkeys share are:\n ",
',\n '.join(repr(ws) for ws in wake_stats))</langsyntaxhighlight>
 
{{out}}
Line 1,812 ⟶ 1,810:
===Python: Recursive===
 
<langsyntaxhighlight lang="python">def wake_and_split(n0, sailors, depth=None):
if depth is None:
depth = sailors
Line 1,836 ⟶ 1,834:
nuts = monkey_coconuts(sailors)
print("For %i sailors the initial nut count is %i" % (sailors, nuts))
</syntaxhighlight>
</lang>
 
{{out}}
Line 1,844 ⟶ 1,842:
===by solving Diophantine equation===
The following is a more or less general solution for arbitrary number of sailors and varying numbers of coconuts the monkey gets. The monkey can be given more coconuts than there are sailors each turn. This is not part of task requirement.
<langsyntaxhighlight lang="python"># gives one solution of (x,y) for a x + by = c
def dioph(a, b, c):
aa,bb,x,y = a, b, 0, 1
Line 1,887 ⟶ 1,885:
 
# many sailors, many nuts
#for i in range(1, 5): print(10**i, calcnuts([1]*10**i + [0])[0])</langsyntaxhighlight>
 
=={{header|R}}==
The only tricky bit is reading comprehension. For example, it's easy to miss that the coconut count after the final nighttime visit must be strictly positive and divisible by the number of sailors.
<langsyntaxhighlight lang="rsplus">coconutsProblem <- function(sailorCount)
{
stopifnot(sailorCount > 1) #Problem makes no sense otherwise
Line 1,907 ⟶ 1,905:
}
}
print(data.frame("Sailors" = 2:8, "Coconuts" = sapply(2:8, coconutsProblem)))</langsyntaxhighlight>
{{out}}
<pre> Sailors Coconuts
Line 1,920 ⟶ 1,918:
=={{header|Racket}}==
{{trans|Python}}
<langsyntaxhighlight Racketlang="racket">#lang racket
 
(define (wake-and-split nuts sailors depth wakes)
Line 1,944 ⟶ 1,942:
(printf "For ~a sailors the initial nut count is ~a\n" sailors (first (last wakes)))
(map displayln (reverse wakes))
(newline))</langsyntaxhighlight>
{{out}}
<pre>For 5 sailors the initial nut count is 3121
Line 1,970 ⟶ 1,968:
This will test combinations of sailors and coconuts to see if they form a valid pairing. The first 6 are done using brute force, testing every combination until a valid one is found. For cases of 7 to 15 sailors, it uses a carefully crafted filter to drastically reduce the number of trials needed to find a valid case (to one, as it happens... :-) )
 
<syntaxhighlight lang="raku" perl6line>my @ones = flat 'th', 'st', 'nd', 'rd', 'th' xx 6;
my @teens = 'th' xx 10;
my @suffix = lazy flat (@ones, @teens, @ones xx 8) xx *;
Line 2,005 ⟶ 2,003:
 
multi sub coconuts ( $sailors where { $sailors % 2 == 0 } ) { ($sailors - 1) * ($sailors ** $sailors - 1) }
multi sub coconuts ( $sailors where { $sailors % 2 == 1 } ) { $sailors ** $sailors - $sailors + 1 }</langsyntaxhighlight>
{{out}}
<pre>
Line 2,046 ⟶ 2,044:
{{trans|C}} {from the 1<sup>st</sup> '''C''' example}<br>
 
<langsyntaxhighlight lang="rexx">/*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.*/
Line 2,062 ⟶ 2,060:
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.*/</langsyntaxhighlight>
Programming note: &nbsp; The parentheses in the last REXX ('''return''') statement aren't necessary, but help for readability. <br>
 
Line 2,074 ⟶ 2,072:
This REXX version is the same as the above version (but the defaults are different),
<br>and it also eliminates the use of a subroutine, making it faster.
<langsyntaxhighlight lang="rexx">/*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. */
Line 2,085 ⟶ 2,083:
end /*$*/
say 'sailors='n " coconuts="$ /*display number of sailors & coconuts.*/
end /*n*/ /*stick a fork in it, we're all done. */</langsyntaxhighlight>
{{out|output|text=&nbsp; when using the default inputs:}}
<pre>
Line 2,101 ⟶ 2,099:
{{trans|C}} {from the 2<sup>nd</sup> '''C''' example}<br>
 
<langsyntaxhighlight lang="rexx">/*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 " " " " " */
Line 2,118 ⟶ 2,116:
nuts= nuts + 1 + nuts % nn /*bump the number coconuts to the pile.*/
end /*k*/
return nuts /*see if number coconuts>0 & remainder.*/</langsyntaxhighlight>
{{out|output|text=&nbsp; when using the default inputs:}}
<pre>
Line 2,132 ⟶ 2,130:
 
=={{header|Ring}}==
<langsyntaxhighlight lang="ring">
# Project : Sailors, coconuts and a monkey problem
 
Line 2,165 ⟶ 2,163:
next
see "In the morning each sailor gets " + m/sailors + " nuts" + nl + nl
</syntaxhighlight>
</lang>
Output:
<pre>
Line 2,188 ⟶ 2,186:
=={{header|Ruby}}==
===Brute Force===
<langsyntaxhighlight lang="ruby">def valid?(sailor, nuts)
sailor.times do
return false if (nuts % sailor) != 1
Line 2,205 ⟶ 2,203:
n -= 1 + div
end
end</langsyntaxhighlight>
{{out}}
<pre>5 sailors => 3121 coconuts
Line 2,225 ⟶ 2,223:
===Faster version===
{{works with|Ruby|2.1+}}
<langsyntaxhighlight lang="ruby">def coconuts(sailor)
sailor.step(by:sailor) do |nuts|
flag = sailor.times do
Line 2,237 ⟶ 2,235:
(2..9).each do |sailor|
puts "#{sailor}: #{coconuts(sailor)}"
end</langsyntaxhighlight>
{{out}}
<pre>2: 11
Line 2,248 ⟶ 2,246:
9: 387420481</pre>
===A function to find the solution see [[User_talk:Nigel_Galloway#Inflammatory_stuff]] for a description===
<langsyntaxhighlight lang="ruby">def ng (sailors)
def _ng (sailors, iter, start) #a method that given a possible answer applies the constraints of the tale to see if it is correct
n, g = [start], [start/sailors]
Line 2,265 ⟶ 2,263:
end
 
</syntaxhighlight>
</lang>
===A possible use of the function===
<langsyntaxhighlight lang="ruby">(3..10).each{|sailors| puts "Number of sailors = #{sailors}"; p ng(sailors)}</langsyntaxhighlight>
{{out}}
The output consists of two list. The first is the number of nuts in each pile, the second the number of nuts taken by each dishonest sailor. So in the case of three, start with 25 nuts, the first sailor takes 8 and discards 1 leaving a pile of 16. The second sailor takes 5 and discards 1 leaving 10. The last dishonest sailor takes 3 discards 1 leaving 6 nuts, which can be shared equally between the 3 (2 each).
Line 2,286 ⟶ 2,284:
Number of sailors = 10
[[31381059600, 34867844001, 38742048891, 43046720991, 47829689991, 53144099991, 59048999991, 65609999991, 72899999991, 80999999991, 89999999991], [3138105960, 3486784400, 3874204889, 4304672099, 4782968999, 5314409999, 5904899999, 6560999999, 7289999999, 8099999999, 8999999999]]</pre>
Did someone ask the value for 100 sailors?<langsyntaxhighlight lang="ruby">
n = ng(100)
(0..100).each{|g| puts "#{n[0][100-g]}:#{n[1][100-g]}"}</langsyntaxhighlight>
The number of coconuts requires is as follows, the whole output is at [[Sailors, coconuts and a monkey problem/Ruby output 100]]
<pre>9899999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999901</pre>
Line 2,294 ⟶ 2,292:
=={{header|Scala}}==
{{Out}}Best seen running in your browser either by [https://scalafiddle.io/sf/jBSqGXg/0 ScalaFiddle (ES aka JavaScript, non JVM, be patient)] or [https://scastie.scala-lang.org/lZXMc4YBSl2htEBw4D2TUQ Scastie (remote JVM)].
<langsyntaxhighlight Scalalang="scala">object Sailors extends App {
var x = 0
 
Line 2,311 ⟶ 2,309:
}
 
}</langsyntaxhighlight>
 
=={{header|Sidef}}==
{{trans|Bc}}
<langsyntaxhighlight lang="ruby">func coconuts(sailors, monkeys=1) {
if ((sailors < 2) || (monkeys < 1) || (sailors <= monkeys)) {
return 0
Line 2,330 ⟶ 2,328:
2.to(9).each { |sailor|
say "#{sailor}: #{coconuts(sailor)}";
}</langsyntaxhighlight>
{{out}}
<pre>
Line 2,347 ⟶ 2,345:
This is a very straightforward implementation. The "overnight" proc attempts to fulfill the activities of the night, throwing an error (through "assert") if it cannot. "minnuts" keeps trying to call it with more nuts until it succeeds. On success, "overnight" will return a list which narrates the night's activities.
 
<langsyntaxhighlight Tcllang="tcl">proc assert {expr {msg ""}} { ;# for "static" assertions that throw nice errors
if {![uplevel 1 [list expr $expr]]} {
if {$msg eq ""} {
Line 2,395 ⟶ 2,393:
puts "Solution with $n sailors:"
minnuts $n
}</langsyntaxhighlight>
 
{{out}}
Line 2,421 ⟶ 2,419:
{{trans|C}}
For performance reasons, we limit ourselves to seven sailors.
<syntaxhighlight lang="text">For n = 2 To 7
t = 0
For x = 1 Step 1 While t = 0
Line 2,446 ⟶ 2,444:
b@ = b@ + 1 + b@ / a@
Next
Return (b@)</langsyntaxhighlight>
{{out}}
<pre>2: 11 1
Line 2,456 ⟶ 2,454:
 
0 OK, 0:127</pre>
 
=={{header|Uiua}}==
 
<syntaxhighlight lang="Uiua">
# Produce candidate list of final numbers
⊚=0◿5⇡2000
# Five times: only keep piles that can be split by 4, do so,
# multiply by five and add one. Return the first value.
⊢⍥(+1×5÷4⊏⊚=0◿4.)5
 
⊚=0◿6⇡100000
⊢⍥(+1×6÷5⊏⊚=0◿5.)6
</syntaxhighlight>
{{out}}
<pre>
3121
233275
</langpre>
 
Or for a terse point-free loop through different numbers of sailors:
<syntaxhighlight lang="Uiua">
+2⇡7
≡(
↘1×,⇡10000000
⊟:⊢⍥(+1×+1⟜÷⟜(⊏⊚=0◿)-1:,,),
)
</syntaxhighlight>
{{out}}
<pre>
╭─
╷ 2 11
3 25
4 765
5 3121
6 233275
7 823537
8 117440505
</langpre>
 
=={{header|VBA}}==
<langsyntaxhighlight lang="vb">Option Explicit
Public Sub coconuts()
Dim sailors As Integer
Line 2,486 ⟶ 2,523:
Debug.Print sailors, minimum, finalshare
Next sailors
End Sub</langsyntaxhighlight>{{out}}
<pre>Sailors Pile Final share
2 11 1
Line 2,496 ⟶ 2,533:
=={{header|Wren}}==
{{trans|Kotlin}}
<langsyntaxhighlight ecmascriptlang="wren">var coconuts = 11
for (ns in 2..9) {
var hidden = List.filled(ns, 0)
Line 2,522 ⟶ 2,559:
coconuts = coconuts + ns
}
}</langsyntaxhighlight>
 
{{out}}
Line 2,605 ⟶ 2,642:
=={{header|Yabasic}}==
{{trans|D}}
<langsyntaxhighlight Yabasiclang="yabasic">coconuts = 11
 
for ns = 2 to 9
Line 2,631 ⟶ 2,668:
coconuts = coconuts + ns
loop
next</langsyntaxhighlight>
 
=={{header|zkl}}==
{{trans|Python}}
<langsyntaxhighlight lang="zkl">fcn monkey_coconuts(sailors=5){
nuts,wakes:=sailors,List();
while(True){
Line 2,658 ⟶ 2,695:
println("On each waking, the nut count, portion taken, and monkeys share are:\n ",
wake_stats.concat("\n "));
}</langsyntaxhighlight>
{{out}}
<pre>
Line 2,680 ⟶ 2,717:
</pre>
{{trans|C}}
<langsyntaxhighlight lang="zkl">fcn total(n, nuts){
nuts *= n;
foreach k in (n){
Line 2,695 ⟶ 2,732:
break;
}
}</langsyntaxhighlight>
{{out}}
<pre>
67

edits