aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMohammad S Anwar <Mohammad.Anwar@yahoo.com>2020-09-26 00:37:36 +0100
committerGitHub <noreply@github.com>2020-09-26 00:37:36 +0100
commit06da11b1253cb302dc451ecf36b5d74201b74a67 (patch)
tree6360c52769ea590a2c4cc0c0ecdffba788ff7d77
parent9705f076c0958bf9479957cc9c8f2a4fbe0f7cf1 (diff)
parent413fd12ced09a5c435e4180527e8134156fd0c13 (diff)
downloadperlweeklychallenge-club-06da11b1253cb302dc451ecf36b5d74201b74a67.tar.gz
perlweeklychallenge-club-06da11b1253cb302dc451ecf36b5d74201b74a67.tar.bz2
perlweeklychallenge-club-06da11b1253cb302dc451ecf36b5d74201b74a67.zip
Merge pull request #2375 from jo-37/contrib
Found silver bullet
-rwxr-xr-xchallenge-079/jo-37/perl/ch-1.pl14
-rwxr-xr-x[-rw-r--r--]challenge-079/jo-37/perl/ch-2.pl0
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