diff options
| author | Mohammad S Anwar <Mohammad.Anwar@yahoo.com> | 2020-09-25 09:13:11 +0100 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2020-09-25 09:13:11 +0100 |
| commit | 80a00a8d1c7a3d0be6779b61371e4b7454d896a4 (patch) | |
| tree | a56467f5867ff6757cb729db0953147a397e626e | |
| parent | 6276d4544a1c25bde6ee3e58d7c594d53681bafd (diff) | |
| parent | f560b4fb29001797e418e4b002ddf3d2ba9f288d (diff) | |
| download | perlweeklychallenge-club-80a00a8d1c7a3d0be6779b61371e4b7454d896a4.tar.gz perlweeklychallenge-club-80a00a8d1c7a3d0be6779b61371e4b7454d896a4.tar.bz2 perlweeklychallenge-club-80a00a8d1c7a3d0be6779b61371e4b7454d896a4.zip | |
Merge pull request #2373 from jo-37/contrib
Add modulus
| -rwxr-xr-x | challenge-079/jo-37/perl/ch-1.pl | 15 |
1 files changed, 11 insertions, 4 deletions
diff --git a/challenge-079/jo-37/perl/ch-1.pl b/challenge-079/jo-37/perl/ch-1.pl index c44f5c75b4..67ec7adfa8 100755 --- a/challenge-079/jo-37/perl/ch-1.pl +++ b/challenge-079/jo-37/perl/ch-1.pl @@ -3,11 +3,11 @@ use Test2::V0; no warnings 'recursion'; use bigint; +use constant MOD => 1000000007; # 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. The modulus to 1000000007 is ignored as a number with -# that many bits is far beyond anything manageable. +# calculation. sub bitsum { my $n = shift; @@ -33,7 +33,14 @@ sub bitsum { $offset + ($allone == $offset - 1 ? 2 * bitsum($allone) : bitsum($allone) + bitsum($offset - 1)); } -is bitsum(4), 5, 'first example'; -is bitsum(3), 4, 'second example'; +# Get the modulus. +sub bitsum_mod { + return 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'; done_testing; |
