aboutsummaryrefslogtreecommitdiff
path: root/challenge-079/dave-cross/perl/ch-1.pl
blob: c137165bb2763f4b95f69068953ad0077e521384 (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
#!/usr/bin/perl

use strict;
use warnings;
use feature qw[say signatures];
no warnings 'experimental::signatures';

if (!@ARGV or !$ARGV[0] or $ARGV[0] =~ /\D/) {
  die "Please give me a positive integer\n";
}

my $n = shift;

my $count;

for (1 .. $n) {
  $count += set_bits_in($_);
}

say "$count % 1000000007 = $count";

sub set_bits_in($decimal) {
  sprintf('%b', $decimal) =~ tr/1/1/;
}