Population count: Difference between revisions
m
→{{header|Perl}}: rearrange and de-clutter
Not a robot (talk | contribs) (Add CLU) |
SqrtNegInf (talk | contribs) m (→{{header|Perl}}: rearrange and de-clutter) |
||
Line 2,980:
<lang perl>use strict;
use warnings;
use feature 'say';
sub population_count { ▼
my $n = shift;▼
my $c;▼
for ($c = 0; $n; $n >>= 1) { ▼
print join ' ', map { population_count(3**$_) } 0 .. 30 - 1;▼
sub evil {
my $i = 0;
sub { $i++ while population_count($i) % 2; $i++ }
}
sub odious {
my $i = 0;
Line 3,002 ⟶ 2,992:
}
▲sub population_count {
my ($evil, $odious) = (evil, odious);▼
my
▲ for ($c = 0; $n; $n >>= 1) { $c += $n & 1 }
push @odious, $odious->();▼
}
printf "Odious: %s\n", join ' ', @odious;</lang>▼
my (@evil, @odious);
▲my ($evil, $odious) = (evil, odious);
▲push( @evil, $evil->() ), push @odious, $odious->() for 1 .. 30;
say "Evil @evil";
{{out}}
<pre>1 2 2 4 3 6 6 5 6 8 9 13 10 11 14 15 11 14 14 17 17 20 19 22 16 18 24 30 25 25
Evil
Odious
A faster population count can be done with pack/unpack:
|