I started with GrandFather's code, rewriting it slightly, then dumped the counts of a series with constant $holes, then looked it up on the OEIS. Hubris, I know.
Looks like the solution is a simple lookup in binomial coefficients (on a diagonal in the Pascal's triangle).
#! /usr/bin/perl -l my $cards = 12; my $holes = 7; sub arrange { my ($n, $k, $i, $prefix) = @_; return "$prefix $n" if ++$i == $k; map arrange($n - $_, $k, $i, "$prefix $_"), (1 .. $n - ($k - $i)); } sub solve { my ($n, $k) = (shift, shift); return [@_, $n] unless (@_ - $k + 1); map solve($n - $_, $k, @_, $_), 1 .. $n + (@_ - $k + 1); } # binomial coefficients ie combinations C(n,k) # (this can be written far more efficiently, of course) sub choose { my ($n, $k) = (shift, shift); return 0 if $k < 0 || $k > $n; !$n || choose($n-1, $k) + choose($n-1, $k-1) } print "@$_" for solve($cards, $holes); print "== ", int(()=solve($cards, $holes)); print "@{[map {int (()=solve($holes + $_, $holes))} 0..17]}"; print "@{[map {choose($holes -1 + $_, $_)} 0..17]}";
Update. Here's a solution with Algorithm::Combinatorics.
#! /usr/bin/perl -l use Algorithm::Combinatorics ':all'; my $cards = 12; my $holes = 7; my $iter = combinations_with_repetition( [1 .. $holes], $cards - $hole +s ); while (my $x = $iter->next) { print "@{distrib($holes, $x)}"; } sub distrib { my @d = (1) x shift; ++$d[$_-1] for @{+shift}; return \@d; }
In reply to Re: Combinatorics problem. (Updated with more info.)
by oiskuu
in thread Combinatorics problem. (Updated with more info.)
by BrowserUk
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |