Beefy Boxes and Bandwidth Generously Provided by pair Networks
The stupid question is the question not asked
 
PerlMonks  

Re: Combinatorics problem. (NestedLoops)

by tye (Sage)
on Dec 11, 2015 at 15:16 UTC ( [id://1150044]=note: print w/replies, xml ) Need Help??


in reply to Combinatorics problem. (Updated with more info.)

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"; }

- tye        

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://1150044]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others contemplating the Monastery: (5)
As of 2024-03-29 14:16 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found