diff options
| -rw-r--r-- | challenge-148/james-smith/perl/ch-1.pl | 50 | ||||
| -rw-r--r-- | challenge-148/james-smith/perl/ch-2.pl | 4 |
2 files changed, 45 insertions, 9 deletions
diff --git a/challenge-148/james-smith/perl/ch-1.pl b/challenge-148/james-smith/perl/ch-1.pl index 3bd0a56989..653e93ed85 100644 --- a/challenge-148/james-smith/perl/ch-1.pl +++ b/challenge-148/james-smith/perl/ch-1.pl @@ -9,6 +9,13 @@ use Benchmark qw(cmpthese timethis); use Data::Dumper qw(Dumper); my $N = @ARGV ? $ARGV[0] : 1; +$N<0?(method_one(-$N)):(method_two($N)); + +sub method_zero { + my @e = map { my $a=$_; map {$a+$_}(0,2,4,6) }(0,30,40,50,60); + shift @e; + say join "\n",@e; +} ## works for $N up to 7 (numbers up to but not including 1 sextillion) ## There are no additional numbers for $N = 8 and 9. @@ -18,15 +25,36 @@ my $N = @ARGV ? $ARGV[0] : 1; ## as we have no solutions for numbers containing sextillion, septillion ## decillion to novemdecillion .. -my @digits = (0,2,4,6); -my @tens = (0,30,40,50,60); - +## Units for which there are no "e" are used: +## 0 not-spelled at all except for Zero +## 2 +## 4 +## 6 +## Tens for which there a no "e": +## 0 not-spelled at all (except when 0) +## 30 +## 40 +## 50 +## 60 ## We only need to find 2 digit eban numbers here as there are no 3 ## digit eban numbers - hundred contains an "e"... -## We need to dump our first eban numbers! -say join "\n",(my@e=grep{$_}my@n=map{my$a=$_;map{$a+$_}@digits}@tens); +sub method_one { + say for my@e=grep{$_}my@n=map{my$a=$_;map{$a+$_}(0,2,4,6)}(0,30,40,50,60); + for(2..$_[0]) { + say for @e=map{my$a=$_;map{sprintf'%s,%03d',$a,$_}@n}@e; + } +} + +## Slight optimization - we avoid sprintf which is sub-optimal +sub method_two { + say for my@e=grep{$_}map{0+$_}(my@n=map{my$a=$_;map{'0'.$a.$_}(0,2,4,6)}(0,3..6)); + #say for my@e=map{0+$_}@n[1..@n-1]; + for(2..$_[0]) { + say for @e=map{my$a=$_;map{$a.','.$_}@n}@e; + } +} ######################################################################## ## Now we extend these by adding more digits at the end so we get those @@ -34,8 +62,6 @@ say join "\n",(my@e=grep{$_}my@n=map{my$a=$_;map{$a+$_}@digits}@tens); ## Note there would need to be a slight tweak when we get to sextillion, ## septillion to skip those numbers -say join"\n",(@e=map{my$a=$_;map{sprintf'%s,%03d',$a,$_}@n}@e)for 2..$N; - ######################################################################## ## Just as an aside the number of eban numbers for $N is precisely ## 20^$N - 1 (up to including $N==7) @@ -45,3 +71,13 @@ say join"\n",(@e=map{my$a=$_;map{sprintf'%s,%03d',$a,$_}@n}@e)for 2..$N; ## specify where eban numbers can be negative - if this is the case any ## +ve eban number has an associated -ve eban number - and visa-versa. +# | Max | (in words) | Rate method 1 | Rate method 2 | %diff (2v1) | +# | ----: | :---------: | ------------: : ------------: : ----------: | +# | 10^3 | Thousand | 200,481 /s | 104,559 /s | -48% | +# | 10^6 | Million | 6,996 /s | 10,311 /s | 47% | +# | 10^9 | Billion | 343 /s | 500a /s | 46% | +# | 10^12 | Trillion | 15.4 /s | 26.2 /s | 70% | +# | ----: | :---------: | ------------: : ------------: : ----------: | +# | 10^15 | Quadrillion | 1.57 s | 0.811 s | 94% | +# | 10^18 | Quintillion | 29.5 s | 16.7 s | 77% | + diff --git a/challenge-148/james-smith/perl/ch-2.pl b/challenge-148/james-smith/perl/ch-2.pl index a7509916b9..b56a4f9aa2 100644 --- a/challenge-148/james-smith/perl/ch-2.pl +++ b/challenge-148/james-smith/perl/ch-2.pl @@ -34,7 +34,7 @@ use Data::Dumper qw(Dumper); ## It is an integer if $n%$d is zero. Again saves rounding error issues -for my $k (1..3333) { +for my $k (1..333) { for( my ($b, $n) = (1, $k*$k*(8*$k-3) ); $n > $b*$b; $b++ ) { say join "\t", 3*$k-1,$b,$n/$b/$b unless $n%($b*$b); } @@ -45,7 +45,7 @@ for my $k (1..3333) { sub is_card { my($a,$b,$c) = @_; - return abs( cr($a+$b*sqrt$c) + cr($a-$b*sqrt$) - 1 ) < 0.000001; + return abs( cr($a+$b*sqrt$c) + cr($a-$b*sqrt$c) - 1 ) < 0.000001; } ## To get the cube route - the code would fail if the value |
