aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMohammad S Anwar <Mohammad.Anwar@yahoo.com>2020-09-23 18:30:46 +0100
committerGitHub <noreply@github.com>2020-09-23 18:30:46 +0100
commit8f17860cdbfc651371074115c1e2b010793eb4bd (patch)
treea187a94048d7c214a13c419ca9f78bb1a4e9bb60
parent8fa4acfae67dcf037675971ca48c73ef9460191e (diff)
parent2219aec5c27fb1757c5cde240adf6c0fa5231179 (diff)
downloadperlweeklychallenge-club-8f17860cdbfc651371074115c1e2b010793eb4bd.tar.gz
perlweeklychallenge-club-8f17860cdbfc651371074115c1e2b010793eb4bd.tar.bz2
perlweeklychallenge-club-8f17860cdbfc651371074115c1e2b010793eb4bd.zip
Merge pull request #2360 from oWnOIzRi/week079
add solution week 79 task 1
-rw-r--r--challenge-079/steven-wilson/perl/ch-1.pl28
1 files changed, 28 insertions, 0 deletions
diff --git a/challenge-079/steven-wilson/perl/ch-1.pl b/challenge-079/steven-wilson/perl/ch-1.pl
new file mode 100644
index 0000000000..bd3a6f20b8
--- /dev/null
+++ b/challenge-079/steven-wilson/perl/ch-1.pl
@@ -0,0 +1,28 @@
+#!/usr/bin/env perl
+# TASK #1 › Count Set Bits
+# Submitted by: Mohammad S Anwar
+#
+# 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.
+
+use strict;
+use warnings;
+use List::Util qw/ sum /;
+use feature qw/ say /;
+
+my $num = $ARGV[0];
+my $cat_binary_strings;
+
+die "Number was not positive" if $num < 1;
+
+for (1 .. $num) {
+ $cat_binary_strings .= sprintf("%b", $_);
+}
+
+my $total_set_bit_count = sum( split //, $cat_binary_strings );
+
+say $total_set_bit_count % 1000000007;
+