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;
}
-
Are you posting in the right place? Check out Where do I post X? to know for sure.
-
Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
<code> <a> <b> <big>
<blockquote> <br /> <dd>
<dl> <dt> <em> <font>
<h1> <h2> <h3> <h4>
<h5> <h6> <hr /> <i>
<li> <nbsp> <ol> <p>
<small> <strike> <strong>
<sub> <sup> <table>
<td> <th> <tr> <tt>
<u> <ul>
-
Snippets of code should be wrapped in
<code> tags not
<pre> tags. In fact, <pre>
tags should generally be avoided. If they must
be used, extreme care should be
taken to ensure that their contents do not
have long lines (<70 chars), in order to prevent
horizontal scrolling (and possible janitor
intervention).
-
Want more info? How to link
or How to display code and escape characters
are good places to start.