diff options
| author | Myoungjin JEON <jeongoon@gmail.com> | 2020-09-27 23:13:27 +1000 |
|---|---|---|
| committer | Myoungjin JEON <jeongoon@gmail.com> | 2020-09-27 23:13:27 +1000 |
| commit | 563b5318ee80297fa540206e8a2a921c0096d67e (patch) | |
| tree | 11e624810023a72a638b3ef557fe47764106fbac /challenge-079 | |
| parent | 07d740d18cb7f0e9e9a80ecff1a6b6e8b4751ce3 (diff) | |
| parent | 92fcbdda975a6fbafab2a1330e3b09c1d731879d (diff) | |
| download | perlweeklychallenge-club-563b5318ee80297fa540206e8a2a921c0096d67e.tar.gz perlweeklychallenge-club-563b5318ee80297fa540206e8a2a921c0096d67e.tar.bz2 perlweeklychallenge-club-563b5318ee80297fa540206e8a2a921c0096d67e.zip | |
Merge remote-tracking branch 'upstream/master'
Diffstat (limited to 'challenge-079')
25 files changed, 1262 insertions, 16 deletions
diff --git a/challenge-079/athanasius/perl/ch-1.pl b/challenge-079/athanasius/perl/ch-1.pl new file mode 100644 index 0000000000..c264c58bd1 --- /dev/null +++ b/challenge-079/athanasius/perl/ch-1.pl @@ -0,0 +1,179 @@ +#!perl + +############################################################################### +=comment + +Perl Weekly Challenge 079 +========================= + +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 representa- +tions of all numbers from 1 to $N and return $total_count_set_bit % 1000000007. + +Example 1: + + Input: $N = 4 + + Explanation: First find out the set bit counts of all numbers i.e. 1, 2, 3 and + 4. + + Decimal: 1 + Binary: 001 + Set Bit Counts: 1 + + Decimal: 2 + Binary: 010 + Set Bit Counts: 1 + + Decimal: 3 + Binary: 011 + Set Bit Counts: 2 + + Decimal: 4 + Binary: 100 + Set Bit Counts: 1 + + Total set bit count: 1 + 1 + 2 + 1 = 5 + + Output: Your script should print `5` as `5 % 1000000007 = 5`. + +Example 2: + + 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`. + +=cut +############################################################################### + +#--------------------------------------# +# Copyright © 2020 PerlMonk Athanasius # +#--------------------------------------# + +#============================================================================== +=comment + +Notes +----- +The set bit count sums for numbers 0, 1, 2, ... are given by the sequence +"A000788 Total number of 1's in binary expansions of 0, ..., n." in "The On- +Line Encyclopedia of Integer Sequences" [ https://oeis.org/A000788 ]. + +The first 63 terms (i.e., for n = 0 to 62) are: +0, 1, 2, 4, 5, 7, 9, 12, 13, 15, 17, 20, 22, 25, 28, 32, 33, 35, 37, 40, 42, +45, 48, 52, 54, 57, 60, 64, 67, 71, 75, 80, 81, 83, 85, 88, 90, 93, 96, 100, +102, 105, 108, 112, 115, 119, 123, 128, 130, 133, 136, 140, 143, 147, 151, 156, +159, 163, 167, 172, 176, 181, 186. + +Additional terms up to n = 10000 are given in the file "b000788.txt" +[ https://oeis.org/A000788/b000788.txt ]. Some examples (useful for testing): + + --------------------- + n A000788(n) + --------------------- + 252 1,002 + 1,879 10,004 + 6,494 40,007 + 10,000 64,613 + --------------------- + +=cut +#============================================================================== + + # Exports: +use strict; +use warnings; +use Const::Fast; # const() +use Regexp::Common qw( number ); # %RE{num} + +const my $MODULUS => 1_000_000_007; +const my $USAGE => +"Usage: + perl $0 <N> + + <N> A positive integer\n"; + +#------------------------------------------------------------------------------ +BEGIN +#------------------------------------------------------------------------------ +{ + $| = 1; + print "\nChallenge 079, Task #1: Count Set Bits (Perl)\n\n"; +} + +#============================================================================== +MAIN: +#============================================================================== +{ + my $N = parse_command_line(); + + print "Input: \$N = $N\n"; + + # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - + # Code adapted from: + # David W. Wilson, "Fast C++ function for computing a(n)", + # https://oeis.org/A000788/a000788.txt + # + # unsigned A000788(unsigned n) + # { + # unsigned v = 0; + # for (unsigned bit = 1; bit <= n; bit <<= 1) + # v += ((n>>1)&~(bit-1)) + ((n&bit) ? (n&((bit<<1)-1))-(bit-1) : 0); + # return v; + # } + # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - + + my $total_count_set_bit = 0; + + for (my $bit = 1; $bit <= $N; $bit <<= 1) + { + $total_count_set_bit += ($N >> 1) & ~($bit - 1); + + $total_count_set_bit += ($N & (($bit << 1) - 1)) - ($bit - 1) + if $N & $bit; + } + + # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - + + printf "Output: \$total_count_set_bit %% %d = %d\n", + $MODULUS, $total_count_set_bit % $MODULUS; +} + +#------------------------------------------------------------------------------ +sub parse_command_line +#------------------------------------------------------------------------------ +{ + my $args = scalar @ARGV; + $args == 1 or die "ERROR: Incorrect number ($args) of " . + "command-line arguments\n$USAGE"; + my $N = $ARGV[0]; + $N =~ /\A$RE{num}{int}\z/ or die "ERROR: Non-integer '$N'\n$USAGE"; + $N >= 0 or die "ERROR: $N is negative\n$USAGE"; + + return $N; +} + +############################################################################### diff --git a/challenge-079/athanasius/perl/ch-2.pl b/challenge-079/athanasius/perl/ch-2.pl new file mode 100644 index 0000000000..161a75cc2b --- /dev/null +++ b/challenge-079/athanasius/perl/ch-2.pl @@ -0,0 +1,219 @@ +#!perl + +############################################################################### +=comment + +Perl Weekly Challenge 079 +========================= + +Task #2 +------- +*Trapped Rain Water* + +Submitted by: Mohammad S Anwar + +You are given an array of positive numbers @N. + +Write a script to represent it as Histogram Chart and find out how much water +it can trap. + +Example 1: + +Input: @N = (2, 1, 4, 1, 2, 5) +The histogram representation of the given array is as below. + + 5 # + 4 # # + 3 # # + 2 # # # # + 1 # # # # # # + _ _ _ _ _ _ _ + 2 1 4 1 2 5 + +Looking at the above histogram, we can see, it can trap 1 unit of rain water +between 1st and 3rd column. Similary it can trap 5 units of rain water between +3rd and last column. + +Therefore your script should print 6. + +Example 2: + +Input: @N = (3, 1, 3, 1, 1, 5) +The histogram representation of the given array is as below. + + 5 # + 4 # + 3 # # # + 2 # # # + 1 # # # # # # + _ _ _ _ _ _ _ + 3 1 3 1 1 5 + +Looking at the above histogram, we can see, it can trap 2 units of rain water +between 1st and 3rd column. Also it can trap 4 units of rain water between 3rd +and last column. + +Therefore your script should print 6. + +=cut +############################################################################### + +#--------------------------------------# +# Copyright © 2020 PerlMonk Athanasius # +#--------------------------------------# + +#============================================================================== +=comment + +Notes +----- +To imagine the histogram trapping water, we have to also imagine (transparent!) +walls in front and behind. It seems perfectly in keeping to also imagine a +floor beneath row 1. Therefore, rows with 0 height can still trap water, pro- +vided there are walls to the left and right. + +The algorithm used is naive but simple (and therefore straightforward to imple- +ment): for every empty square in the histogram, determine whether there are +walls to its left and right; if so, it can trap rain water. + +Note that no water can be trapped in the left-most or right-most columns. + +=cut +#============================================================================== + + # Exports: +use strict; +use warnings; +use Const::Fast; # const() +use List::Util qw( max ); +use Regexp::Common qw( number ); # %RE{num} + +#------------------------------------------------------------------------------ +# Constants +#------------------------------------------------------------------------------ + +const my $MAX_COLUMNS => 38; # (For my particular command-line screen setup) +const my $MAX_HEIGHT => 31; # N.B.: The logic in print_histogram() below + # assumes that $MAX_HEIGHT < 100 +const my $USAGE => +"Usage: + perl $0 [<N> ...] + + [<N> ...] A non-empty array of positive integers\n"; + +#------------------------------------------------------------------------------ +BEGIN +#------------------------------------------------------------------------------ +{ + $| = 1; + print "\nChallenge 079, Task #2: Trapped Rain Water (Perl)\n\n"; +} + +#============================================================================== +MAIN: +#============================================================================== +{ + my @N = parse_command_line(); + + printf "Input: \@N = (%s)\n\n", join ', ', @N; + print "Histogram representation:\n\n"; + + print_histogram(@N); + + my $water = 0; + + for my $col (1 .. $#N - 1) + { + for my $row ($N[$col] + 1 .. max @N) + { + ++$water if is_wall_left (\@N, $col, $row) && + is_wall_right(\@N, $col, $row); + } + } + + print "\nThis histogram can trap $water units of rain water\n"; +} + +#------------------------------------------------------------------------------ +sub is_wall_left +#------------------------------------------------------------------------------ +{ + my ($N, $column, $row) = @_; + + for my $col (reverse(0 .. $column - 1)) + { + return 1 if $N->[$col] >= $row; + } + + return 0; +} + +#------------------------------------------------------------------------------ +sub is_wall_right +#------------------------------------------------------------------------------ +{ + my ($N, $column, $row) = @_; + + for my $col ($column + 1 .. $#$N) + { + return 1 if $N->[$col] >= $row; + } + + return 0; +} + +#------------------------------------------------------------------------------ +# From Perl Weekly Challenge #075, Task 2: Largest Rectangle Histogram +# +sub print_histogram +#------------------------------------------------------------------------------ +{ + my @N = @_; + my $columns = scalar @N; + my $max_height = max @N; + + if ($columns <= $MAX_COLUMNS && + $max_height <= $MAX_HEIGHT) + { + for my $row (reverse 1 .. $max_height) + { + printf " %2d", $row; + print $_ >= $row ? ' #' : ' ' for @N; + print "\n"; + } + + printf " _%s\n", ' _' x $columns; + + if ($max_height < 10) + { + printf " %s\n", join ' ', @N; + } + else + { + printf " %s\n", join ' ', map { int($_ / 10) || ' ' } @N; + printf " %s\n", join ' ', map { $_ % 10 } @N; + } + } + else + { + printf "The histogram is too %s to print on a single screen\n", + $columns > $MAX_COLUMNS ? 'wide' : 'tall'; + } +} + +#------------------------------------------------------------------------------ +sub parse_command_line +#------------------------------------------------------------------------------ +{ + scalar @ARGV > 0 or die "ERROR: Missing command-line arguments\n" . + "$USAGE"; + for (@ARGV) + { + /\A$RE{num}{int}\z/ or die "ERROR: Non-integer '$_'\n$USAGE"; + $_ >= 0 or die "ERROR: $_ is negative\n$USAGE"; + } + + return @ARGV; +} + +############################################################################### diff --git a/challenge-079/athanasius/raku/ch-1.raku b/challenge-079/athanasius/raku/ch-1.raku new file mode 100644 index 0000000000..f726047845 --- /dev/null +++ b/challenge-079/athanasius/raku/ch-1.raku @@ -0,0 +1,164 @@ +use v6d; + +############################################################################### +=begin comment + +Perl Weekly Challenge 079 +========================= + +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 representa- +tions of all numbers from 1 to $N and return $total_count_set_bit % 1000000007. + +Example 1: + + Input: $N = 4 + + Explanation: First find out the set bit counts of all numbers i.e. 1, 2, 3 and + 4. + + Decimal: 1 + Binary: 001 + Set Bit Counts: 1 + + Decimal: 2 + Binary: 010 + Set Bit Counts: 1 + + Decimal: 3 + Binary: 011 + Set Bit Counts: 2 + + Decimal: 4 + Binary: 100 + Set Bit Counts: 1 + + Total set bit count: 1 + 1 + 2 + 1 = 5 + + Output: Your script should print `5` as `5 % 1000000007 = 5`. + +Example 2: + + 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`. + +=end comment +############################################################################### + +#--------------------------------------# +# Copyright © 2020 PerlMonk Athanasius # +#--------------------------------------# + +#============================================================================== +=begin comment + +Notes +----- +The set bit count sums for numbers 0, 1, 2, ... are given by the sequence +"A000788 Total number of 1's in binary expansions of 0, ..., n." in "The On- +Line Encyclopedia of Integer Sequences" [ https://oeis.org/A000788 ]. + +The first 63 terms (i.e., for n = 0 to 62) are: +0, 1, 2, 4, 5, 7, 9, 12, 13, 15, 17, 20, 22, 25, 28, 32, 33, 35, 37, 40, 42, +45, 48, 52, 54, 57, 60, 64, 67, 71, 75, 80, 81, 83, 85, 88, 90, 93, 96, 100, +102, 105, 108, 112, 115, 119, 123, 128, 130, 133, 136, 140, 143, 147, 151, 156, +159, 163, 167, 172, 176, 181, 186. + +Additional terms up to n = 10000 are given in the file "b000788.txt" +[ https://oeis.org/A000788/b000788.txt ]. Some examples (useful for testing): + + --------------------- + n A000788(n) + --------------------- + 252 1,002 + 1,879 10,004 + 6,494 40,007 + 10,000 64,613 + --------------------- + +=end comment +#============================================================================== + +my UInt constant $MODULUS = 1_000_000_007; + +#------------------------------------------------------------------------------ +BEGIN +#------------------------------------------------------------------------------ +{ + "\nChallenge 079, Task #1: Count Set Bits (Raku)\n".put; +} + +##============================================================================= +sub MAIN +( + UInt:D $N #= A positive integer +) +##============================================================================= +{ + "Input: \$N = $N".put; + + # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - + # Code adapted from: + # David W. Wilson, "Fast C++ function for computing a(n)", + # https://oeis.org/A000788/a000788.txt + # + # unsigned A000788(unsigned n) + # { + # unsigned v = 0; + # for (unsigned bit = 1; bit <= n; bit <<= 1) + # v += ((n>>1)&~(bit-1)) + ((n&bit) ? (n&((bit<<1)-1))-(bit-1) : 0); + # return v; + # } + # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - + + my UInt $total-count-set-bit = 0; + + loop (my UInt $bit = 1; $bit <= $N; $bit +<= 1) + { + $total-count-set-bit += ($N +> 1) +& +^($bit - 1); + + $total-count-set-bit += ($N +& (($bit +< 1) - 1)) - ($bit - 1) + if $N +& $bit; + } + + # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - + + "Output: \$total-count-set-bit %% %d = %d\n".printf: $MODULUS, + $total-count-set-bit % $MODULUS; +} + +#------------------------------------------------------------------------------ +sub USAGE() +#------------------------------------------------------------------------------ +{ + my Str $usage = $*USAGE; + + $usage ~~ s/ ($*PROGRAM-NAME) /raku $0/; + $usage.put; +} + +############################################################################## diff --git a/challenge-079/athanasius/raku/ch-2.raku b/challenge-079/athanasius/raku/ch-2.raku new file mode 100644 index 0000000000..e2c990bbb3 --- /dev/null +++ b/challenge-079/athanasius/raku/ch-2.raku @@ -0,0 +1,194 @@ +use v6d; + +############################################################################### +=begin comment + +Perl Weekly Challenge 079 +========================= + +Task #2 +------- +*Trapped Rain Water* + +Submitted by: Mohammad S Anwar + +You are given an array of positive numbers @N. + +Write a script to represent it as Histogram Chart and find out how much water +it can trap. + +Example 1: + +Input: @N = (2, 1, 4, 1, 2, 5) +The histogram representation of the given array is as below. + + 5 # + 4 # # + 3 # # + 2 # # # # + 1 # # # # # # + _ _ _ _ _ _ _ + 2 1 4 1 2 5 + +Looking at the above histogram, we can see, it can trap 1 unit of rain water +between 1st and 3rd column. Similary it can trap 5 units of rain water between +3rd and last column. + +Therefore your script should print 6. + +Example 2: + +Input: @N = (3, 1, 3, 1, 1, 5) +The histogram representation of the given array is as below. + + 5 # + 4 # + 3 # # # + 2 # # # + 1 # # # # # # + _ _ _ _ _ _ _ + 3 1 3 1 1 5 + +Looking at the above histogram, we can see, it can trap 2 units of rain water +between 1st and 3rd column. Also it can trap 4 units of rain water between 3rd +and last column. + +Therefore your script should print 6. + +=end comment +############################################################################### + +#--------------------------------------# +# Copyright © 2020 PerlMonk Athanasius # +#--------------------------------------# + +#============================================================================== +=begin comment + +Notes +----- +To imagine the histogram trapping water, we have to also imagine (transparent!) +walls in front and behind. It seems perfectly in keeping to also imagine a +floor beneath row 1. Therefore, rows with 0 height can still trap water, pro- +vided there are walls to the left and right. + +The algorithm used is naive but simple (and therefore straightforward to imple- +ment): for every empty square in the histogram, determine whether there are +walls to its left and right; if so, it can trap rain water. + +Note that no water can be trapped in the left-most or right-most columns. + +=end comment +#============================================================================== + +my UInt constant $MAX-COLUMNS = 38; # (For my particular command-line setup) +my UInt constant $MAX-HEIGHT = 31; # N.B.: The logic in print-histogram() + # below assumes $MAX-HEIGHT < 100 + +#------------------------------------------------------------------------------ +BEGIN +#------------------------------------------------------------------------------ +{ + "\nChallenge 079, Task #2: Trapped Rain Water (Raku)\n".put; +} + +##============================================================================= +sub MAIN +( + *@N where { @N.elems > 0 && #= A non-empty array of positive integers + .all ~~ UInt:D } +) +##============================================================================= +{ + "Input: \@N = (%s)\n\n".printf: @N.join: ', '; + "Histogram representation:\n".put; + + print-histogram(@N); + + my UInt $water = 0; + + for 1 .. @N.end - 1 -> UInt $col + { + for @N[$col] + 1 .. @N.max -> UInt $row + { + ++$water if is-wall-left( @N, $col, $row) && + is-wall-right(@N, $col, $row); + } + } + + "\nThis histogram can trap $water units of rain water".put; +} + +#------------------------------------------------------------------------------ +sub is-wall-left(Array:D[UInt:D] $N, UInt:D $column, UInt:D $row --> Bool:D) +#------------------------------------------------------------------------------ +{ + for (0 .. $column - 1).reverse -> UInt $col + { + return True if $N[$col] >= $row; + } + + return False; +} + +#------------------------------------------------------------------------------ +sub is-wall-right(Array:D[UInt:D] $N, UInt:D $column, UInt:D $row --> Bool:D) +#------------------------------------------------------------------------------ +{ + for $column + 1 .. $N.end -> UInt $col + { + return True if $N[$col] >= $row; + } + + return False; +} + +#------------------------------------------------------------------------------ +# From Perl Weekly Challenge #075, Task 2: Largest Rectangle Histogram +# +sub print-histogram(Array:D[UInt:D] $A) +#------------------------------------------------------------------------------ +{ + my UInt $columns = $A.elems; + my UInt $max-height = $A.max; + + if $columns <= $MAX-COLUMNS && + $max-height <= $MAX-HEIGHT + { + for (1 .. $max-height).reverse -> UInt $row + { + ' %2d'.printf: $row; + ' %s' .printf: $_ >= $row ?? '#' !! ' ' for $A.list; + ''.put; + } + + " _%s\n".printf: ' _' x $columns; + + if $max-height < 10 + { + " %s\n".printf: $A.join: ' '; + } + else + { + " %s\n".printf: $A.map( { ($_ / 10).floor || ' ' } ).join: ' '; + " %s\n".printf: $A.map( { $_ % 10 } ).join: ' '; + } + } + else + { + "The histogram is too %s to print on a single screen\n".printf: + $columns > $MAX-COLUMNS ?? 'wide' !! 'tall'; + } +} + +#------------------------------------------------------------------------------ +sub USAGE() +#------------------------------------------------------------------------------ +{ + my Str $usage = $*USAGE; + + $usage ~~ s/ ($*PROGRAM-NAME) /raku $0/; + $usage.put; +} + +############################################################################### diff --git a/challenge-079/colin-crain/blog.txt b/challenge-079/colin-crain/blog.txt new file mode 100644 index 0000000000..978c0f4c4d --- /dev/null +++ b/challenge-079/colin-crain/blog.txt @@ -0,0 +1 @@ +https://colincrain.wordpress.com/2020/09/26/count-set-match-standing-water-in-mountain-pools/ diff --git a/challenge-079/jeongoon/perl/blog1.txt b/challenge-079/jeongoon/blog1.txt index 2ce996bf47..2ce996bf47 100644 --- a/challenge-079/jeongoon/perl/blog1.txt +++ b/challenge-079/jeongoon/blog1.txt diff --git a/challenge-079/jo-37/perl/ch-1.pl b/challenge-079/jo-37/perl/ch-1.pl index 67ec7adfa8..b2ac754190 100755 --- a/challenge-079/jo-37/perl/ch-1.pl +++ b/challenge-079/jo-37/perl/ch-1.pl @@ -3,8 +3,11 @@ use Test2::V0; no warnings 'recursion'; use bigint; +use Memoize; use constant MOD => 1000000007; +memoize 'bitsum'; + # Calculate the sum of 1-bits in all numbers from 0 to n. Going from 0 # to n instead of 1 to n does not change the result, but simplifies the # calculation. @@ -30,17 +33,18 @@ sub bitsum { # When splitting a full power-of-two part, both sub-parts to be # recursed into are the same. This leads to a shortcut for at least # half of the calculations. - $offset + ($allone == $offset - 1 ? 2 * bitsum($allone) : bitsum($allone) + bitsum($offset - 1)); + $offset + ($allone == $offset - 1 ? + 2 * bitsum($allone) : + bitsum($allone) + bitsum($offset - 1)); } -# Get the modulus. +# Apply the modulus. sub bitsum_mod { - return bitsum(shift) % MOD; + bitsum(shift) % MOD; } is bitsum_mod(4), 5, 'first example'; is bitsum_mod(3), 4, 'second example'; -ok +(bitsum(1e9) > MOD), 'large bitsum'; -ok +(bitsum_mod(1e9) < MOD), 'large bitsum with modulus'; +is bitsum_mod(77347884), 1, 'silver bullet'; done_testing; diff --git a/challenge-079/jo-37/perl/ch-2.pl b/challenge-079/jo-37/perl/ch-2.pl index 40f585a472..40f585a472 100644..100755 --- a/challenge-079/jo-37/perl/ch-2.pl +++ b/challenge-079/jo-37/perl/ch-2.pl diff --git a/challenge-079/juliodcs/perl/ch-1.pl b/challenge-079/juliodcs/perl/ch-1.pl index 5409ee8c5a..cf72b9f9b9 100644 --- a/challenge-079/juliodcs/perl/ch-1.pl +++ b/challenge-079/juliodcs/perl/ch-1.pl @@ -14,16 +14,13 @@ use strict; use warnings; use feature 'say'; use experimental 'signatures'; +use bigint; use constant MODULE => 1000000007; -use constant INTEGER_LAST => 2**63 - 1; - -use constant W_ACCURATE => 'Warning: intermediate result > Integer\'last. Result *may* not be accurate!'; -use constant E_ACCURATE => 'Error: Number cannot be > Integer\'last'; use constant E_NO_NUMBER => 'You need to submit a number'; sub length_bin($number) { - length sprintf '%b', $number; + Math::BigInt->new($number)->blog(2) + 1 } # Given a number, it calculates the flips of the most-significant-bit number @@ -39,10 +36,7 @@ sub next_number($number) { } sub calculate ( $number, $total = 0 ) { - if ( $number == 0 ) { - say {*STDERR} W_ACCURATE if $total > INTEGER_LAST; - return $total % MO |
