GrandFather spake it well; this question maps quite well into a recursive algorithm:
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 );
I was quite surprised to see how quickly the 10, 10 case started giving results, even on a lowly 2.4GHz Xeon...