diff options
| author | drbaggy <js5@sanger.ac.uk> | 2020-11-02 09:18:20 +0000 |
|---|---|---|
| committer | drbaggy <js5@sanger.ac.uk> | 2020-11-02 09:18:20 +0000 |
| commit | 9fd87b20eba89ba32d2cd02d7747ef8f1600ceac (patch) | |
| tree | fcf31606f7306aedf1c46a4a608b8e4ee7e29c6e /challenge-085/james-smith/perl | |
| parent | 531ddb021e0c3718813f68208a54d27bf7c9b15f (diff) | |
| download | perlweeklychallenge-club-9fd87b20eba89ba32d2cd02d7747ef8f1600ceac.tar.gz perlweeklychallenge-club-9fd87b20eba89ba32d2cd02d7747ef8f1600ceac.tar.bz2 perlweeklychallenge-club-9fd87b20eba89ba32d2cd02d7747ef8f1600ceac.zip | |
more testing and some optimization
Diffstat (limited to 'challenge-085/james-smith/perl')
| -rw-r--r-- | challenge-085/james-smith/perl/ch-1.pl | 2 | ||||
| -rw-r--r-- | challenge-085/james-smith/perl/ch-2.pl | 23 |
2 files changed, 18 insertions, 7 deletions
diff --git a/challenge-085/james-smith/perl/ch-1.pl b/challenge-085/james-smith/perl/ch-1.pl index 025b30c9e1..352ddbfcdd 100644 --- a/challenge-085/james-smith/perl/ch-1.pl +++ b/challenge-085/james-smith/perl/ch-1.pl @@ -12,7 +12,9 @@ done_testing; sub trip_sum { for my $i (0.. (@_-3) ) { + next unless $_[$i] < 2; ## For large arrays this might save some time...; for my $j ( ($i+1).. (@_-2) ) { + next unless $_[$i]+$_[$j] < 2; ## For large arrays this might save some time...; for ( ($j+1) .. (@_-1) ) { return 1 if 1 < $_[$i]+$_[$j]+$_[$_] && $_[$i]+$_[$j]+$_[$_] < 2; } diff --git a/challenge-085/james-smith/perl/ch-2.pl b/challenge-085/james-smith/perl/ch-2.pl index 782f4ec5b1..be15852334 100644 --- a/challenge-085/james-smith/perl/ch-2.pl +++ b/challenge-085/james-smith/perl/ch-2.pl @@ -4,19 +4,28 @@ use strict; use warnings; use Test::More; -is( is_power(8), 1); -is( is_power(15), 0); -is( is_power(125), 1); +is( is_power(1), 1); +is( is_power(8), 1); +is( is_power(15), 0); +is( is_power(125), 1); +is( is_power(21943**3), 1); +is( is_power(4294967295), 0); +is( is_power(2**60), 1); +is( is_power(225), 1); done_testing; sub is_power { + ## Avoid using powers as this is inefficient... my $n = shift; + return 1 if $n == 1; ## This is an edge case! for(my $i=2;1;$i++) { - last if 2**$i>$n; - for($_=1;1;$_++) { - return 1 if $n == $_**$i; - last if $_**$i>$n; + my $s = $i*$i; + last if $s>$n; + for($_=0;1;$_++) { + return 1 if $s == $n; + last if $n%$s; + $s*=$i; } } return 0; |
