aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-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;
+}
+