aboutsummaryrefslogtreecommitdiff
path: root/challenge-079
diff options
context:
space:
mode:
authorWalt Mankowski <waltman@pobox.com>2020-09-21 20:43:20 -0400
committerWalt Mankowski <waltman@pobox.com>2020-09-21 20:43:20 -0400
commit96602a860bf1744e423f0d41222ff82d70812c0f (patch)
tree2038cd5760c4a03fed269a6fb9d656e769bb67e7 /challenge-079
parent3c6bacdb27568a0b3708e032c79f80b6999b02b9 (diff)
downloadperlweeklychallenge-club-96602a860bf1744e423f0d41222ff82d70812c0f.tar.gz
perlweeklychallenge-club-96602a860bf1744e423f0d41222ff82d70812c0f.tar.bz2
perlweeklychallenge-club-96602a860bf1744e423f0d41222ff82d70812c0f.zip
perl code for challenge 79 task 1
Diffstat (limited to 'challenge-079')
-rw-r--r--challenge-079/walt-mankowski/perl/ch-1.pl23
1 files changed, 23 insertions, 0 deletions
diff --git a/challenge-079/walt-mankowski/perl/ch-1.pl b/challenge-079/walt-mankowski/perl/ch-1.pl
new file mode 100644
index 0000000000..10bc99650f
--- /dev/null
+++ b/challenge-079/walt-mankowski/perl/ch-1.pl
@@ -0,0 +1,23 @@
+#!/usr/bin/env perl
+use strict;
+use warnings;
+use feature qw(:5.32);
+use experimental qw(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.
+
+sub bits_set($x) {
+ my $bin = sprintf "%b", $x;
+ return $bin =~ tr/1//;
+}
+
+my $MOD = 1000000007;
+my $n = $ARGV[0];
+my $sum = 0;
+$sum += bits_set($_) for 1..$n;
+
+say $sum, " ", $sum % $MOD;