Another approach to the end is thus:my @n = (-25, 14, 50, 20, -7, -8, -10); my $max = 2**@n; for (my $i = 0; $i < $max; ++$i) { my $sum = 0; $sum += $n[$_] for grep $i & 2**$_, 0 .. $#n; if ($sum == 0) { my ($bits, @used) = $i; while ($bits) { my $high_bit = int(log($bits)/log(2)); push @used, $n[$high_bit]; $bits &= ~(2**$high_bit); } print "[@used] = 0\n"; } }
which could also be written as a for loop:my ($bits, $j, @used) = ($i, 0); while ($bits) { push @used, $n[$j] if $bits & 1; $bits >>= 1, ++$j; }
for ( my ($bits, $j, @used) = ($i, 0); $bits; $bits >>= 1, ++$j ) { push @used, $n[$j] if $bits & 1; }
_____________________________________________________
Jeff[japhy]Pinyan:
Perl,
regex,
and perl
hacker, who'd like a job (NYC-area)
s++=END;++y(;-P)}y js++=;shajsj<++y(p-q)}?print:??;
In reply to Re: permuation algorithm
by japhy
in thread permutation algorithm
by Basilides
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |