exercism-perl5

Repository for my Perl 5 Exercism exercises
git clone git://git.samirparikh.com/exercism-perl5
Log | Files | Refs

commit d12e409401189202c94559f57e072a237a831e42
parent 5eec787cdd468d78827ef9725162f54a62f5e3d1
Author: Samir Parikh <noreply@samirparikh.com>
Date:   Thu, 25 Nov 2021 02:32:33 +0000

update SumOfMultiples.pm to get all tests to pass

Diffstat:
Msum-of-multiples/SumOfMultiples.pm | 18++++++++++++------
1 file changed, 12 insertions(+), 6 deletions(-)

diff --git a/sum-of-multiples/SumOfMultiples.pm b/sum-of-multiples/SumOfMultiples.pm @@ -12,16 +12,22 @@ sub sum_of_multiples { my ($input) = @_; my @factors = @{$input->{factors}}; my $limit = $input->{limit}; + my %multiples = (); my $sum_of_multiples = 0; - #print "@factors\n"; - print join ":", @factors; + return $sum_of_multiples unless (@factors); # return 0 if empty array foreach my $factor (@factors) { - # if current factor is greater than or equal to the limit, - # skip to the next one + # skip if current factor is greater than or equal to the limit, next if $factor >= $limit; + next unless ($factor); # skip if factor equals 0 + my $current = $factor; + while ($current < $limit) { + $multiples{$current} = $current; # add a multiple to the hash + $current += $factor; + } + } + foreach my $key (keys %multiples) { + $sum_of_multiples += $key; } - #print "$limit\n"; - #return undef; return $sum_of_multiples; }