diff options
| author | Abigail <abigail@abigail.freedom.nl> | 2022-01-25 20:17:25 +0100 |
|---|---|---|
| committer | Abigail <abigail@abigail.freedom.nl> | 2022-01-25 20:17:25 +0100 |
| commit | a678bf9e5c29c258c31e906b2b9dd911b919b1ac (patch) | |
| tree | 0b74a64a6dece3bf0310ac010a9a2ff1d6c1bbbb | |
| parent | f6f8fa27a551ad1171389cc9ca8b5c9a0e39e025 (diff) | |
| download | perlweeklychallenge-club-a678bf9e5c29c258c31e906b2b9dd911b919b1ac.tar.gz perlweeklychallenge-club-a678bf9e5c29c258c31e906b2b9dd911b919b1ac.tar.bz2 perlweeklychallenge-club-a678bf9e5c29c258c31e906b2b9dd911b919b1ac.zip | |
Week 149, part 1: Use same algorithm for Perl solution.
When calculating the sum of the digits, use the modulo and divide
method, instead of stringifying and summing the digits extracted
with a regexp.
| -rw-r--r-- | challenge-149/abigail/perl/ch-1.pl | 12 |
1 files changed, 11 insertions, 1 deletions
diff --git a/challenge-149/abigail/perl/ch-1.pl b/challenge-149/abigail/perl/ch-1.pl index ca72dbefbf..0f4127cb8a 100644 --- a/challenge-149/abigail/perl/ch-1.pl +++ b/challenge-149/abigail/perl/ch-1.pl @@ -29,7 +29,17 @@ use List::Util qw [sum]; # # Return the sum of the digits of its argument # -sub digitsum ($n) {sum $n =~ /\d/ag} +sub digitsum ($number) { + my $sum = 0; + my $base = 10; + while ($number > 0) { + use integer; + $sum += $number % $base; + $number /= $base; + } + return $sum; +} + # # Return whether the argument is a Fibonacci number. We do this by |
