Just off the top of my head, this makes me think of $c-$p nested loops to pick slots for the surplus cards like:
my $p = 7; # Number of pigeon holes.
my $c = 12; # Number of cards
for my $s1 ( 1..$p ) {
for my $s2 ( $s1..$p ) {
for my $s3 ( $s2..$p ) {
... Need $c-$p loops
Which translates to simple code if I don't worry about being more efficient, but it also handles your 7,12 "instantly" for me.
#!/usr/bin/perl -w
use strict;
use Algorithm::Loops 'NestedLoops';
my( $p, $c ) = @ARGV;
my $iter = NestedLoops(
[ [ 1..$p ], ( sub { [ $_..$p ] } ) x ($c-$p-1) ],
);
my @s;
while( @s = $iter->() ) {
my @p = (1) x $p;
$p[$_-1]++ for @s;
print "@p\n";
}
-
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.
|