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

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.