#! perl -slw use strict; sub _next { my ($v) = @_; my $t = ($v | ($v - 1)) + 1; return ($t | (((($t & -$t) / ($v & -$v)) >> 1) - 1)); } sub permIt { my $l = @_; my @chars = grep $_ ne '-', @_; my $n = my $s = ( 1 << @chars ) -1; return sub { return undef if $n > ( $s << ( $l - @chars ) ); my $bits = pack 'Q', $n; my @copy = @chars; my $str = join '', map{ vec( $bits, $_, 1 ) ? shift( @copy ) : '-' } 0 .. $l-1; $n = _next( $n ); return $str; }; } my $iter = permIt( split '', $ARGV[0] ); #print $_ while defined( $_ = $iter->() ); #__END__ my $n = 0; printf "\r%d\t", ++$n while defined( $_ = $iter->() ); print $n; __END__ C:\test>1059792 ABCDEFGHIJKLMNOPQRSTUVWXYZ------ 906192 906192 | 1| 2| 3| 4| 5| 6| 7| 8| 9| -+--+--+---+----+----+----+-----+-----+-----+ 1| 2| 3| 4| 5| 6| 7| 8| 9| 10| 2| 3| 6| 10| 15| 21| 28| 36| 45| 55| 3| 4|10| 20| 35| 56| 84| 120| 165| 220| 4| 5|15| 35| 70| 126| 210| 330| 495| 715| 5| 6|21| 56| 126| 252| 462| 792| 1281| 2002| 6| 7|28| 84| 210| 462| 929| 1716| 3003| 5005| 7| 8|36|120| 330| 792|1716| 3432| 6435|11440| 8| 9|45|165| 495|1287|3003| 6435|12870|24310| 9|10|55|220| 715|2002|5005|11440|24310|48620| 10|11|66|286|1001|3003|8008|19448|43758|92378|