From a678bf9e5c29c258c31e906b2b9dd911b919b1ac Mon Sep 17 00:00:00 2001 From: Abigail Date: Tue, 25 Jan 2022 20:17:25 +0100 Subject: 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. --- challenge-149/abigail/perl/ch-1.pl | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) 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 -- cgit