Solve hanging lantern problem: Difference between revisions
Content added Content deleted
(Created Nim solution.) |
(→{{header|Ruby}}: Add Ruby) |
||
Line 1,390: | Line 1,390: | ||
[6,5,4,3,1,2] |
[6,5,4,3,1,2] |
||
[6,5,4,3,2,1]</pre> |
[6,5,4,3,2,1]</pre> |
||
=={{header|Ruby}}== |
|||
===Directly computing the count=== |
|||
If 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|Wren}}== |
=={{header|Wren}}== |