Combinations with repetitions: Difference between revisions

m
m (→‎{{header|Sidef}}: updated code)
Line 2,899:
=={{header|Sidef}}==
{{trans|Perl}}
<lang ruby>func pcwr (n, al, l)a {= n>0 ? (l.range.map{p(n-1, a+[l[_]], l.ft(_))}) : a };{
func f (n) { n>0 ? (n^l *-> fmap {|k| __FUNC__(n -1, 1l.slice(k), [a..., l[k]]) }) : 1 };a
func n (n, m) { f(n + m - 1) / f(n) / f(m - 1) };
 
p(2, [], %w(iced jam plain)).each { |a|
say a.map{|pair| pair.join(" ")}.join("\n");
}
 
pcwr(2, [], %w(iced jam plain)).each { |a|
printf("\nThere are %d ways to pick 7 out of 10\n", n(7, 10));</lang>
say a.map{|pair| pair.join("' "') }.join("\n");
}</lang>
 
Also built-in:
 
<lang ruby>%w(iced jam plain).combinations_with_repetition(2, {|*a|
say a.join(' ')
})</lang>
 
{{out}}
Line 2,917 ⟶ 2,921:
jam plain
plain plain
</pre>
 
Efficient count of the total number of combinations with repetition:
There are 11440 ways to pick 7 out of 10
<lang ruby>func cwr_count (n, m) { binomial(n + m - 1, m) }
printf("\nThere are %ds ways to pick 7 out of 10 with repetition\n", ncwr_count(710, 107));</lang>
{{out}}
<pre>
There are 11440 ways to pick 7 out of 10 with repetition
</pre>
 
2,756

edits