diff options
| author | drbaggy <js5@sanger.ac.uk> | 2022-03-29 15:39:20 +0100 |
|---|---|---|
| committer | drbaggy <js5@sanger.ac.uk> | 2022-03-29 15:39:20 +0100 |
| commit | 3bfd401364eb88675dfe45726aa5e776ef562b42 (patch) | |
| tree | 793edfa0cb272115dbcc068e46a1dfaedf8f1056 | |
| parent | 71c522cd99e1930f91b2a8333888d287bb7c76da (diff) | |
| download | perlweeklychallenge-club-3bfd401364eb88675dfe45726aa5e776ef562b42.tar.gz perlweeklychallenge-club-3bfd401364eb88675dfe45726aa5e776ef562b42.tar.bz2 perlweeklychallenge-club-3bfd401364eb88675dfe45726aa5e776ef562b42.zip | |
x
| -rw-r--r-- | challenge-158/james-smith/README.md | 25 | ||||
| -rw-r--r-- | challenge-158/james-smith/perl/ch-1.pl | 12 |
2 files changed, 36 insertions, 1 deletions
diff --git a/challenge-158/james-smith/README.md b/challenge-158/james-smith/README.md index 74a5faa077..e11f4ed587 100644 --- a/challenge-158/james-smith/README.md +++ b/challenge-158/james-smith/README.md @@ -49,6 +49,31 @@ sub additive_primes { * The *increment* block is called at the end of the loop and so stores the value of `$p` if it is an additive prime, then it increments the loop with the next prime. * Rather than doing a split and sum we use repeated dividing and summing, as it is more efficient around 20-30% more efficient. The increased performance is probably due to avoiding the "duality" of perl variables storing numbers as numbers/strings. +## Extra code + +Rewritten with single line for ... +```perl +sub additive_primes_div { + my @res; + for(my$p=2; my$s=0,(my $q=$p)<=$N;(is_prime$s)&&(push@res,$p),$p=next_prime$p) { + do { $s += $q % 10 } while $q = int $q / 10; + } + @res; +} +``` + +Alternative form with `for split`: + +```perl +sub additive_primes_split { + my @res; + for( my $p = 2; my $s = 0, $p <= $N ; $p = next_prime $p ) { + $s+=$_ for split //, $p; + push @res, $p if is_prime $s; + } + @res; +} +``` # Challenge 2 - First series buban primes ***Write a script to compute first series cuban primes <= 1000. (First series cuban primes have the form `((x+1)^3-x^3)/(x+1-x)` = `3x^2+3x+1`)*** diff --git a/challenge-158/james-smith/perl/ch-1.pl b/challenge-158/james-smith/perl/ch-1.pl index 1473ccb3cb..dd43aa3bc6 100644 --- a/challenge-158/james-smith/perl/ch-1.pl +++ b/challenge-158/james-smith/perl/ch-1.pl @@ -6,16 +6,19 @@ use warnings; use feature qw(say); use Math::Prime::Util qw(next_prime is_prime); use Benchmark qw(cmpthese); +use List::Util qw(sum0); my $N = shift || 100; -my $I = 5e6/$N; +my $I = 1e7/$N; $I = 100 if $I<100; say join ', ', additive_primes_div(); say join ', ', additive_primes_div_expanded(); say join ', ', additive_primes_split(); +say join ', ', additive_primes_split_sum0(); cmpthese( $I, { 'split' => sub { additive_primes_split(); }, + 'sum0' => sub { additive_primes_split_sum0(); }, 'div' => sub { additive_primes_div(); }, 'div_exp' => sub { additive_primes_div_expanded(); }, } ); @@ -49,4 +52,11 @@ sub additive_primes_split { } @res; } +sub additive_primes_split_sum0 { + my @res; + for( my $p = 2; $p <= $N ; $p = next_prime $p ) { + push @res, $p if is_prime sum0 split //, $p; + } + @res; +} |
