Combinations with repetitions: Difference between revisions
Content added Content deleted
m (→{{header|Sidef}}: updated code) |
|||
Line 2,899: | Line 2,899: | ||
=={{header|Sidef}}== |
=={{header|Sidef}}== |
||
{{trans|Perl}} |
{{trans|Perl}} |
||
<lang ruby>func |
<lang ruby>func cwr (n, l, a = []) { |
||
n>0 ? (^l -> map {|k| __FUNC__(n-1, l.slice(k), [a..., l[k]]) }) : a |
|||
func n (n, m) { f(n + m - 1) / f(n) / f(m - 1) }; |
|||
⚫ | |||
⚫ | |||
} |
} |
||
⚫ | |||
⚫ | |||
⚫ | |||
}</lang> |
|||
Also built-in: |
|||
<lang ruby>%w(iced jam plain).combinations_with_repetition(2, {|*a| |
|||
say a.join(' ') |
|||
})</lang> |
|||
{{out}} |
{{out}} |
||
Line 2,917: | Line 2,921: | ||
jam plain |
jam plain |
||
plain plain |
plain plain |
||
</pre> |
|||
Efficient count of the total number of combinations with repetition: |
|||
⚫ | |||
<lang ruby>func cwr_count (n, m) { binomial(n + m - 1, m) } |
|||
⚫ | |||
{{out}} |
|||
<pre> |
|||
⚫ | |||
</pre> |
</pre> |
||