diff options
| -rwxr-xr-x | challenge-079/jo-37/perl/ch-1.pl | 14 | ||||
| -rwxr-xr-x[-rw-r--r--] | challenge-079/jo-37/perl/ch-2.pl | 0 |
2 files changed, 9 insertions, 5 deletions
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 |
