aboutsummaryrefslogtreecommitdiff
path: root/challenge-113/james-smith
diff options
context:
space:
mode:
authordrbaggy <js5@sanger.ac.uk>2021-05-18 06:58:43 +0100
committerdrbaggy <js5@sanger.ac.uk>2021-05-18 06:58:43 +0100
commitd744fbd007daf178752280c7eb5827bedddb6057 (patch)
treec9ab3e1a464b37b423de9e2bd76112520bbbe117 /challenge-113/james-smith
parent16edd6150b08e3247722843e8f9069eade984331 (diff)
downloadperlweeklychallenge-club-d744fbd007daf178752280c7eb5827bedddb6057.tar.gz
perlweeklychallenge-club-d744fbd007daf178752280c7eb5827bedddb6057.tar.bz2
perlweeklychallenge-club-d744fbd007daf178752280c7eb5827bedddb6057.zip
add solution when one of the numbers doesn't end in $d
Diffstat (limited to 'challenge-113/james-smith')
-rw-r--r--challenge-113/james-smith/perl/ch-1.pl14
1 files changed, 13 insertions, 1 deletions
diff --git a/challenge-113/james-smith/perl/ch-1.pl b/challenge-113/james-smith/perl/ch-1.pl
index efb023fcf0..d8445c8f47 100644
--- a/challenge-113/james-smith/perl/ch-1.pl
+++ b/challenge-113/james-smith/perl/ch-1.pl
@@ -7,7 +7,7 @@ use feature qw(say);
use Test::More;
use Benchmark qw(timethis);
-my @ex = ( [25,8,0], [25,7,0], [24,7,1], [24,0,0], [10,0,1], [28,8,1], [26,8,1], [16,8,0], [441,9,1], [431,9,0] );
+my @ex = ( [25,8,0], [25,7,0], [24,7,1], [24,0,0], [10,0,1], [28,8,1], [26,8,1], [16,8,0], [441,9,1], [431,9,1] );
is( represent( $_->[0], $_->[1]), $_->[2] ) foreach @ex;
@@ -17,6 +17,18 @@ timethis( 1_000_000, sub { represent($_->[0],$_->[1]) for @ex } );
say '';
sub represent {
my($t,$n,$d) = (0,@_);
+ ## If $d is equal to 0
+ ## Any number between 100 & 109 can be represented by itself
+ ## For numbers over 109 we can represent these as 100-109 + a
+ ## number ending in 0...
+ ## If $n is between 10*$d and 10*$d+9 then it can be represented as $d$x
+ ## For numbers > than this we can do a similar trick to above
+ ## We can reprent them as $d$x + a number ending in $d
+ return 1 if $n >= 10 * ($d||10);
+ ## Finally we get to the list of numbers less than this - as the only
+ ## digit that can contain $d is the last one we just try to see if
+ ## we can find a sum of numbers ending in $d which have the same last
+ ## digit as $n and less than or equal to $n.
$n>=($t+=$_*10+$d) && ($n%10 == $t%10) && return 1 for 0..9;
0;
}