diff options
| author | Mohammad S Anwar <Mohammad.Anwar@yahoo.com> | 2020-09-24 12:18:51 +0100 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2020-09-24 12:18:51 +0100 |
| commit | 9c8a16968a3b7d2285e99f43af9190fc99be9103 (patch) | |
| tree | 8fbb73a734bc9aa271b5e000e3e907bfa20919a4 | |
| parent | fa61e79d8cd5ea9ff11e2883f4ff649fac0c4c39 (diff) | |
| parent | 3dee9de223167c959c3edca316a4e8476dcf8d7d (diff) | |
| download | perlweeklychallenge-club-9c8a16968a3b7d2285e99f43af9190fc99be9103.tar.gz perlweeklychallenge-club-9c8a16968a3b7d2285e99f43af9190fc99be9103.tar.bz2 perlweeklychallenge-club-9c8a16968a3b7d2285e99f43af9190fc99be9103.zip | |
Merge pull request #2363 from Cris-HD/branch-for-challenge-079
added ch-1.pl
| -rwxr-xr-x | challenge-079/cristian-heredia/perl/ch-1.pl | 59 |
1 files changed, 59 insertions, 0 deletions
diff --git a/challenge-079/cristian-heredia/perl/ch-1.pl b/challenge-079/cristian-heredia/perl/ch-1.pl new file mode 100755 index 0000000000..30e2136aa5 --- /dev/null +++ b/challenge-079/cristian-heredia/perl/ch-1.pl @@ -0,0 +1,59 @@ +#You are given a positive number $N. +#Write a script to count the total number of set bits of the binary representations of all numbers from 1 to $N and return $total_count_set_bit % 1000000007. + + #Input: $N = 3 + + #Explanation: First find out the set bit counts of all numbers i.e. 1, 2 and 3. + + # Decimal: 1 + # Binary: 01 + # Set Bit Count: 1 + + # Decimal: 2 + # Binary: 10 + # Set Bit Count: 1 + + # Decimal: 3 + # Binary: 11 + # Set Bit Count: 2 + + # Total set bit count: 1 + 1 + 2 = 4 + + #Output: Your script should print `4` as `4 % 1000000007 = 4`. + +use strict; +use warnings; +use Data::Dumper; + +#variables +my $N = 3; +my $bin; +my @array; +my $count = 0; + + +convertBinary(); +sub convertBinary { + + foreach(my $i = 1; $i <= $N; $i++) { + $bin = sprintf ("%b", $i); + @array = (); + @array = split //, $bin; + countBit(); + } + + print "output: $count\n"; +} + +sub countBit { + + foreach(my $j = 0; $j < @array; $j++) { + if ($array[$j] == 1) { + $count++; + } + } +} + + + + |
