aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorvinodk89 <vinodkk89@gmail.com>2020-09-21 18:27:57 +0530
committervinodk89 <vinodkk89@gmail.com>2020-09-21 18:27:57 +0530
commit38e0c835523f6017293d39e6ab32ea4c296d0463 (patch)
tree8b532a28cb8637f6fdf0f4709fc8b45879782e08
parent5ac16ac7e9826137e0da5597e954f4992c66205d (diff)
downloadperlweeklychallenge-club-38e0c835523f6017293d39e6ab32ea4c296d0463.tar.gz
perlweeklychallenge-club-38e0c835523f6017293d39e6ab32ea4c296d0463.tar.bz2
perlweeklychallenge-club-38e0c835523f6017293d39e6ab32ea4c296d0463.zip
Solutions for challenge 79
-rw-r--r--challenge-079/vinod-k/ch-1.pl31
1 files changed, 31 insertions, 0 deletions
diff --git a/challenge-079/vinod-k/ch-1.pl b/challenge-079/vinod-k/ch-1.pl
new file mode 100644
index 0000000000..5e844a17dc
--- /dev/null
+++ b/challenge-079/vinod-k/ch-1.pl
@@ -0,0 +1,31 @@
+#!/usr/bin/perl
+
+use strict;
+use warnings;
+
+my $N = $ARGV[0] || 4;
+my $bit_count;
+
+foreach my $i( 1..$N ){
+
+ $bit_count += getSetBit($i);
+}
+
+print "For $N Set Bits are: $bit_count\n";
+print "`$bit_count` as `$bit_count % 1000000007` = ".$bit_count % 1000000007;
+print "\n";
+
+sub getSetBit {
+ my $n = shift;
+
+ my $bin = sprintf ("%b", $n);
+
+ my @bits = split(//, $bin);
+
+ my $count = 0;
+ foreach my $each_n( @bits ){
+ $count++ if( $each_n == 1);
+ }
+ return $count;
+}
+