my $ans = 0; my $total = 0; my $bit; for my $num (0 .. 16777215) { $bit = 1; $total += $_ ? $bit++ : -$bit++ for unpack("b24", pack "L", $num) =~ /./g; $ans++ unless $total; $total = 0; } print "$ans\n";