diff options
Diffstat (limited to 'challenge-079/sgreen/perl/ch-1b.pl')
| -rwxr-xr-x | challenge-079/sgreen/perl/ch-1b.pl | 28 |
1 files changed, 28 insertions, 0 deletions
diff --git a/challenge-079/sgreen/perl/ch-1b.pl b/challenge-079/sgreen/perl/ch-1b.pl new file mode 100755 index 0000000000..ed0628ca60 --- /dev/null +++ b/challenge-079/sgreen/perl/ch-1b.pl @@ -0,0 +1,28 @@ +#!/usr/bin/env perl + +use strict; +use warnings; +use feature 'say'; + +sub main { + my $N = shift; + my $count = 0; + + # Sanity check + die "Please enter a positive integer\n" unless $N; + die "The value '$N' is not a positive integer\n" + unless $N =~ /^[1-9][0-9]*$/; + + my $b = 1; + while ( $b < $N ) { + foreach my $i ( 1 .. $N ) { + $count++ if $i & $b; + } + $b *= 2; + } + + printf qq(%d %% %d = %d\n), $count, 1000000007, $count % 1000000007; +} + +main(@ARGV); + |
