Beefy Boxes and Bandwidth Generously Provided by pair Networks
Syntactic Confectionery Delight
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??

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":



  • 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.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others lurking in the Monastery: (3)
As of 2024-04-25 22:16 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found