From 12438a4961abbf613dff8d2fe15552ee1e54e632 Mon Sep 17 00:00:00 2001 From: drbaggy Date: Thu, 7 Oct 2021 09:25:48 +0100 Subject: add code so dont't try primes greater than root n --- challenge-133/james-smith/perl/ch-2.pl | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/challenge-133/james-smith/perl/ch-2.pl b/challenge-133/james-smith/perl/ch-2.pl index 053d890468..b8f5fc1ec8 100644 --- a/challenge-133/james-smith/perl/ch-2.pl +++ b/challenge-133/james-smith/perl/ch-2.pl @@ -16,7 +16,8 @@ sub sum_prime_factors { my $N = shift; ## If we are composite then store the sum of the digit factors for the composite and return... - ( $N % $_) ? (( $N<$_*$_) && last ) : ( return $sum_pf[$N] = $sum_pf[$N/$_] + $sum_pf[$_] ) + ( $N % $_) ? (( $N<$_*$_) && last ) + : ( return $sum_pf[$N] = $sum_pf[$N/$_] + $sum_pf[$_] ) foreach @primes; ## Otherwise we are prime so add to primes and return nothing.... @@ -40,7 +41,8 @@ sub cl_sum_digits { my $t = 0; $t+=$_ foreach split //, $_[0]; $t } sub cl_sum_prime_factors { my $N = shift; - ( $N % $_) || ( return $sum_pf[$N] = $sum_pf[$N/$_] + $sum_pf[$_] ) foreach @primes; + ( $N % $_) ? (( $N<$_*$_) && last ) + : ( return $sum_pf[$N] = $sum_pf[$N/$_] + $sum_pf[$_] ) foreach @primes; $sum_pf[$N] = cl_sum_digits $N; push @primes, $N; return 0; -- cgit