blob: c7ea428308cb48c2cd0f7b17906fce71a164fd0f (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
|
#!/usr/bin/perl
# Test: ./ch-2.pl 2 3 4
use strict;
use warnings;
use feature qw /say/;
use Algorithm::Combinatorics qw /combinations/;
my $answer = 0;
my $combinations = combinations(\@ARGV, 2);
while (my $v = $combinations->next) {
$answer += f(@$v);
}
say $answer;
sub f {
return calculate_true_bits(int(shift) ^ int(shift));
}
# Calculate the number of true bits
sub calculate_true_bits {
my $number = shift;
my $count = 0;
do {
$count++ if ($number & 1);
} while ($number = $number >> 1);
return $count;
}
|