aboutsummaryrefslogtreecommitdiff
path: root/challenge-079/andinus/README
blob: 1b591981561b0ef7e20351e68f0ec6110ec52f75 (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
43
44
45
                            ━━━━━━━━━━━━━━━
                             CHALLENGE 079

                                Andinus
                            ━━━━━━━━━━━━━━━


Table of Contents
─────────────────

1. Task 1 - Count Set Bits
.. 1. Perl





1 Task 1 - Count Set Bits
═════════════════════════

  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'.


1.1 Perl
────────

  • Program: <file:perl/ch-1.pl>

  We loop from `1 ... $input', convert each `$num' to binary & count the
  set bits & add them to `$set_bits'.
  ┌────
  │ my $input = shift @ARGV;
  │
  │ my $set_bits;
  │ foreach my $num (1 ... $input) {
  │     my $binary = sprintf "%b", $num;
  │     my $count = $binary =~ tr/1//;
  │     $set_bits += $count;
  │ }
  │ print $set_bits % 1000000007, "\n";
  └────