in reply to Re^3: One Zero variants_without_repetition
in thread One Zero variants_without_repetition
I was quite surprised to see how quickly the 10, 10 case started giving results, even on a lowly 2.4GHz Xeon...sub combinatoric($$); # prototype forward reference sub combinatoric( $$ ) { my ( $zero, $one ) = @_; my @ret = (); if ( $one == 0 ) { # no more ones left to select, the rest is zeroes. push @ret, scalar ( '0' x $zero ); } elsif ( $zero == 0 ) { # no more zeroes left to select, the rest is ones. push @ret, scalar ( '1' x $one ); } else { # take a zero from the pile, and compute what's left push @ret, "0$_" foreach combinatoric( $zero-1, $one ); # take a one from the pile, and compute what's left push @ret, "1$_" foreach combinatoric( $zero, $one-1 ); } return @ret; } print "$_\n" foreach combinatoric( 10, 10 );
|
---|
Replies are listed 'Best First'. | |
---|---|
Re^5: One Zero variants_without_repetition (reversion from recursion)
by tye (Sage) on Aug 07, 2007 at 16:29 UTC |