aboutsummaryrefslogtreecommitdiff
path: root/challenge-079/alexander-pankoff/perl/ch-1.pl
blob: 98d0f946a54a2c8e773cd2b9c64acc33e5960e18 (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
33
34
35
36
37
38
39
40
41
42
#!/usr/bin/env perl
use v5.20;
use utf8;
use strict;
use warnings;
use autodie;
use feature qw(say signatures);
no warnings 'experimental::signatures';
#
# You are given a positive number $N.
#
# Write a script to count the total numbrer of set bits of the binary
# representations of all numbers from 1 to $N and return
# $total_count_set_bit % 1000000007.

my ($N) = @ARGV;

say count_set_bits($N) % 1000000007;

sub count_set_bits($n) {
    my $count = 0;
    my $i     = 0;

    while ( (1 << $i) <= $n ) {
        my $set          = 0;
        my $change_after = 1 << $i;
        for ( 0 .. $n ) {
            $count += $set;
            if ( $change_after == 1 ) {
                $set          = !$set;
                $change_after = 1 << $i;
            }
            else {
                $change_after--;

            }
        }
        $i++;
    }

    return $count;
}