aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authordrbaggy <js5@sanger.ac.uk>2022-01-24 15:28:02 +0000
committerdrbaggy <js5@sanger.ac.uk>2022-01-24 15:28:02 +0000
commitd14e65562eb2ae5f5d0a1d26b3643d285ffbd93a (patch)
tree53f55beb56ee1ca77902b4925dcc02739266deac
parent004ffc8ebf81ea5b8525e88c1e0a81a007537579 (diff)
downloadperlweeklychallenge-club-d14e65562eb2ae5f5d0a1d26b3643d285ffbd93a.tar.gz
perlweeklychallenge-club-d14e65562eb2ae5f5d0a1d26b3643d285ffbd93a.tar.bz2
perlweeklychallenge-club-d14e65562eb2ae5f5d0a1d26b3643d285ffbd93a.zip
updated code
-rw-r--r--challenge-149/james-smith/perl/ch-1.pl28
1 files changed, 11 insertions, 17 deletions
diff --git a/challenge-149/james-smith/perl/ch-1.pl b/challenge-149/james-smith/perl/ch-1.pl
index 16f8bbb711..7cd08cdc48 100644
--- a/challenge-149/james-smith/perl/ch-1.pl
+++ b/challenge-149/james-smith/perl/ch-1.pl
@@ -8,23 +8,17 @@ use Test::More;
use Benchmark qw(cmpthese timethis);
use Data::Dumper qw(Dumper);
-my $n = @ARGV ? $ARGV[0] : 20;
-
-my @fib = (0,1,1);
-my %fib = (0,1,1,1);
-
-for(my $i=0; $n; $i++ ) {
- my $ds = 0;
+# As an array we don't need to keep the fibonacci numbers
+# We need them as the keys to the hash %fib which we use
+# to check that a digit sum is a fibonacci number. Instead
+# We only keep the last two values $fa & $fb
+for( my($n,$ds,$i,$fa,$fb,%fib)=(@ARGV?$ARGV[0]:20,0,0,1,1,0,1,1,1);
+ $n; $i++,$ds=0 ) {
$ds+=$_ foreach split //,$i;
- if($ds>$fib[-1]) {
- ## If we dont have a large enough fib add the next one...
- ## Digit sum can only be 1 larger than current maximum
- ## fibonacci.
- push @fib, $fib[-2]+$fib[-1];
- $fib{$fib[-1]}=1;
- }
- next unless exists $fib{$ds};
- say $i;
- $n--;
+ ## If we dont have a large enough fib add the next one...
+ ## Digit sum can only be 1 larger than current maximum
+ ## fibonacci.
+ ($fib{$fa+$fb},$fa,$fb)=(1,$fb,$fa+$fb) if $ds > $fb;
+ (say $i)**$n-- if exists $fib{$ds};
}