diff options
| author | James Smith <js5@sanger.ac.uk> | 2022-02-23 17:12:40 +0000 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2022-02-23 17:12:40 +0000 |
| commit | dc3ce3038fb6d67a8a0562b79a4af78b2794d5b9 (patch) | |
| tree | 1b7170d7a3afb2e4befa1530870e913148131541 /challenge-153 | |
| parent | 811d7b3b2055633ba46c35e9283fd52a0b879266 (diff) | |
| download | perlweeklychallenge-club-dc3ce3038fb6d67a8a0562b79a4af78b2794d5b9.tar.gz perlweeklychallenge-club-dc3ce3038fb6d67a8a0562b79a4af78b2794d5b9.tar.bz2 perlweeklychallenge-club-dc3ce3038fb6d67a8a0562b79a4af78b2794d5b9.zip | |
Update README.md
Diffstat (limited to 'challenge-153')
| -rw-r--r-- | challenge-153/james-smith/README.md | 23 |
1 files changed, 12 insertions, 11 deletions
diff --git a/challenge-153/james-smith/README.md b/challenge-153/james-smith/README.md index 0b9479fefb..0d5719305e 100644 --- a/challenge-153/james-smith/README.md +++ b/challenge-153/james-smith/README.md @@ -87,31 +87,32 @@ sub is_factorion { Running this gives the only 4 factorions: `1`, `2`, `145`, `40585`; -This takes around 3 seconds to run on my test box. To speed this up we can work with groups of 3 digits - so we first create the sum arrays for 1, 2, 3 digits. Note the sum for `20` is different to the sum from `020` - as `0! = 1`. +This takes around 3 seconds to run on my test box. To speed this up we can work with groups of up to 4 digits - so we first create the sum arrays for 1, 2, 3 and 4 digits. Note the sum for `20` is different to the sum from `0020` - as `0! = 1`. The code then becomes: ```perl my @f = (1); push @f, $_*$f[-1] foreach 1..9; +my @z = map { my $t = $_; map {$t+$_} @f } +my @q = map { my $t = $_; map {$t+$_} @f } my @t = map { my $t = $_; map {$t+$_} @f } @f; -my @q = map { my $t = $_; map {$t+$_} @f } @t; -is_factorion_1k($_) && say for 1..2_177_282; +is_factorion_10k($_) && say for 1..2_177_282; -sub is_factorion_1k { +sub is_factorion_10k { my $t = $_[0]; return $t == ( - $t >= 1e6 ? $f[ $t/1e6 ] + $q[ ($t/1e3)%1e3 ] + $q[ $t%1e3 ] + $t >= 1e6 ? $z[ $t/1e3 ] + $q[ $t%1e3 ] : $t >= 1e5 ? $q[ $t/1e3 ] + $q[ $t%1e3 ] : $t >= 1e4 ? $t[ $t/1e3 ] + $q[ $t%1e3 ] - : $t >= 1e3 ? $f[ $t/1e3 ] + $q[ $t%1e3 ] - : $t >= 100 ? $q[ $t ] - : $t >= 10 ? $t[ $t ] - : $f[ $t ] + : $t >= 1e3 ? $z[ $t ] + : $t >= 100 ? $q[ $t ] + : $t >= 10 ? $t[ $t ] + : $f[ $t ] ); } -``` -This comes in at just less than 1 second - and improvement of about 70%. Note the order of the ternaries is important - we start from highest to lowest as it minimizes the average number of comparisions performed. +``` +This comes in at just less than 1 second - a `4x` speed up. Note the order of the ternaries is important - we start from highest to lowest as it minimizes the average number of comparisions performed. |
