Solve hanging lantern problem: Difference between revisions

Added Uiua solution
(Created Nim solution.)
(Added Uiua solution)
(3 intermediate revisions by 2 users not shown)
Line 1,390:
[6,5,4,3,1,2]
[6,5,4,3,2,1]</pre>
=={{header|Ruby}}==
===Directly computing the count===
 
Compute the count directly:
<syntaxhighlight lang="ruby" line>Factorial = Hash.new{|h, k| h[k] = k * h[k-1] } # a memoized factorial
Factorial[0] = 1
 
def count_perms_with_reps(ar)
Factorial[ar.sum] / ar.inject{|prod, m| prod * Factorial[m]}
end
 
ar, input = [], ""
puts "Input column heights in sequence (empty line to end input):"
ar << input.to_i until (input=gets) == "\n"
puts "There are #{count_perms_with_reps(ar)} ways to take these #{ar.size} columns down."
</syntaxhighlight>
{{Out}}
<pre>Input column heights in sequence (empty line to end input):
1
2
3
4
5
6
7
8
 
There are 73566121315513295589120000 ways to take these 8 columns down.
</pre>
 
=={{header|Uiua}}==
{{works with|Uiua|0.10.0}}
<syntaxhighlight lang="Uiua">
Fac ← /×+1⇡
Lant ← ÷⊃(/(×⊙Fac)|Fac/+)
 
Lant [1 2 3]
Lant [1 3 3]
Lant [1 3 3 5 7]
</syntaxhighlight>
{{out}}
<pre>
60
140
5587021440
</pre>
 
=={{header|Wren}}==
Line 1,395 ⟶ 1,441:
{{trans|Python}}
The result for n == 5 is slow to emerge.
<syntaxhighlight lang="ecmascriptwren">var lantern // recursive function
lantern = Fn.new { |n, a|
var count = 0
Line 1,430 ⟶ 1,476:
{{libheader|Wren-big}}
Alternatively, using library methods.
<syntaxhighlight lang="ecmascriptwren">import "./perm" for Perm
import "./big" for BigInt
 
143

edits