aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authordrbaggy <js5@sanger.ac.uk>2022-02-21 14:30:02 +0000
committerdrbaggy <js5@sanger.ac.uk>2022-02-21 14:30:02 +0000
commit6969759d9c3f60d28741151faf35d479fe671cf0 (patch)
treeceabca2201b2e3305afc33293ffa116dd1e3be42
parent606606494f2e723a592bb5b17742351db8aac92b (diff)
downloadperlweeklychallenge-club-6969759d9c3f60d28741151faf35d479fe671cf0.tar.gz
perlweeklychallenge-club-6969759d9c3f60d28741151faf35d479fe671cf0.tar.bz2
perlweeklychallenge-club-6969759d9c3f60d28741151faf35d479fe671cf0.zip
tidied up
-rw-r--r--challenge-153/james-smith/README.md13
-rw-r--r--challenge-153/james-smith/perl/ch-2.pl12
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;
}