diff options
| -rw-r--r-- | challenge-153/james-smith/README.md | 13 | ||||
| -rw-r--r-- | challenge-153/james-smith/perl/ch-2.pl | 12 |
2 files changed, 14 insertions, 11 deletions
diff --git a/challenge-153/james-smith/README.md b/challenge-153/james-smith/README.md index e661a275d5..88a29d84c9 100644 --- a/challenge-153/james-smith/README.md +++ b/challenge-153/james-smith/README.md @@ -70,17 +70,20 @@ Our is factorion function just adds the factorial digit sum and compares to the We start with a pre-computed list of factorials as we only need the values for the integers 0..9; +To avoid having to do a comparison at the end, rather than starting at zero we start at `$n`, so the comparison at the end is just whether or not the sum is `0`. + ```perl -my @FACT = (1); -push @FACT, $_*$FACT[-1] foreach 1..9; +my @f = (1); +push @f, $_*$f[-1] foreach 1..9; is_factorion($_) && say for 1..2_177_282; sub is_factorion { - my $t=0; - $t+=$FACT[$_] for split //,$_[0]; - $t==$_[0]; + my $t = $_[0]; + $t-=$f[$_] for split //,$_[0]; + !$t; } + ``` Running this gives the only 4 factorions: `1`, `2`, `145`, `40585`; diff --git a/challenge-153/james-smith/perl/ch-2.pl b/challenge-153/james-smith/perl/ch-2.pl index 335eba64fc..0eb21c81ef 100644 --- a/challenge-153/james-smith/perl/ch-2.pl +++ b/challenge-153/james-smith/perl/ch-2.pl @@ -8,18 +8,18 @@ use Test::More; use Benchmark qw(cmpthese timethis); use Data::Dumper qw(Dumper); -my @FACT = (1); -push @FACT, $_*$FACT[-1] foreach 1..9; +my @f = (1); +push @f, $_*$f[-1] foreach 1..9; my @TESTS = ( [ 145, 1 ], [ 125, 0 ], ); -is( is_factorion($_->[0]), $_->[1] ) foreach @TESTS; +is( is_factorion($_->[0])||0, $_->[1] ) foreach @TESTS; done_testing(); is_factorion($_) && say for 1 .. 2_177_282; sub is_factorion { - my $t=0; - $t+=$FACT[$_] for split //,$_[0]; - $t==$_[0]; + my $t = $_[0]; + $t-=$f[$_] for split //,$_[0]; + !$t; } |
