aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--challenge-147/james-smith/perl/ch-1-right.pl8
-rw-r--r--challenge-147/james-smith/perl/ch-1.pl76
-rw-r--r--challenge-147/james-smith/perl/ch-2.pl12
3 files changed, 53 insertions, 43 deletions
diff --git a/challenge-147/james-smith/perl/ch-1-right.pl b/challenge-147/james-smith/perl/ch-1-right.pl
index 3668287206..9565a26b0f 100644
--- a/challenge-147/james-smith/perl/ch-1-right.pl
+++ b/challenge-147/james-smith/perl/ch-1-right.pl
@@ -6,10 +6,10 @@ use warnings;
use feature qw(say);
use Time::HiRes qw(time);
-my ($t0,$c,@primes) = ( time, 5, 3 );
+my ( $TEMPLATE, $idx, $t0, $c, @primes) =
+ ( "%6d\t%28d\t%15.6f\n", 0, time, 5, 3 );
my @tprimes_current = (2,3,5,7);
-my $idx = 0;
-printf "%6d\t%28d\t%15.6f\n", ++$idx, $_, time - $t0 foreach @tprimes_current;
+printf $TEMPLATE, ++$idx, $_, time - $t0 foreach @tprimes_current;
for my $p ( 1 .. 100 ) {
last unless @tprimes_current;
@@ -23,7 +23,7 @@ for my $p ( 1 .. 100 ) {
## check if $n is composite;
$n%$_||next B foreach @primes;
push @tprimes_new, $n;
- printf "%6d\t%28d\t%15.6f\n", ++$idx, $n, time - $t0;
+ printf $TEMPLATE, ++$idx, $n, time - $t0;
}
}
@tprimes_current = @tprimes_new;
diff --git a/challenge-147/james-smith/perl/ch-1.pl b/challenge-147/james-smith/perl/ch-1.pl
index 751982aad7..3d9b2ccf72 100644
--- a/challenge-147/james-smith/perl/ch-1.pl
+++ b/challenge-147/james-smith/perl/ch-1.pl
@@ -11,11 +11,10 @@ $|=1;
## Finding left-truncated primes.
## Set up primes
-my( $t0, $index, $N, $c, @primes ) = ( time, 0, @ARGV ? $ARGV[0] : 20, 5, 3 );
+my( $T, $t0, $index, $N, $c, @primes ) =
+ ( "%6d\t%28d\t%15.6f\n", time, 0, @ARGV ? $ARGV[0] : 20, 5, 3 );
my @tprimes_current = (3,7);
-printf "%6d\t%28d\t%15.6f\n", ++$index, $_, time - $t0 foreach 2,3,5,7;
-
## The 1-digit primes are 2, 3, 5 and 7.
## All 2-digit primes (that are truncated) end in either 3 or 7, so we use
## the array (3,7) as the starting list of truncated primes...
@@ -33,30 +32,31 @@ printf "%6d\t%28d\t%15.6f\n", ++$index, $_, time - $t0 foreach 2,3,5,7;
## We continue around this loop looking at longer and longer primes,
## until we have collected enough primes OR @tprimes_current is empty
+printf $T, ++$index, $_, time-$t0 for 2,3,5,7;
while(1) {
last unless @tprimes_current; ## Exit if there are no current l-trunc primes
## We are at the end of the list...
my @tprimes_new = ();
- foreach my $first ( 1..9 ) {
- B: foreach my $base ( @tprimes_current ) {
+ for my $first ( 1..9 ) {
+ B: for my $base ( @tprimes_current ) {
my $p = $first.$base;
## Update list of primes...
- foreach( ;$primes[-1]*$primes[-1]<$p;$c+=2) {
+ for( ;$primes[-1]*$primes[-1]<$p;$c+=2) {
($_*$_>$c)?(push(@primes,$c),last):$c%$_||last for @primes;
}
## check if $p is composite;
- $p%$_||next B foreach @primes;
+ $p%$_||next B for @primes;
## If not we add the prime to the list of primes of length `n+1`
push @tprimes_new, $p;
## And output the index/prime/and time taken..
- printf "%6d\t%28d\t%15.6f\n", ++$index, $p, time - $t0;
+ printf $T, ++$index, $p, time - $t0;
exit if $index >= $N; ## Stop if we have got to limit set
}
}
@@ -82,28 +82,37 @@ while(1) {
## Index Running time Value of prime..
-## 20 0.0001 sec 197
-## 100 0.0005 sec 5,167
-## ----------------------------------------------
-## 500 0.0045 sec 543,853
-## 1,000 0.0173 sec 8,391,283
-## 1,500 0.0477 sec 79,962,683
-## 2,000 0.1246 sec 736,275,167
-## 2,500 0.3298 sec 6,946,986,197
-## 3,000 0.9487 sec 75,315,729,173
-## 3,500 3.9416 sec 1,837,839,918,353
-## 4,000 62.3637 sec 313,231,816,543,853
-## ----------------------------------------------
-## 4,100 3 min 54 sec 2,696,154,867,812,347
-## 4,200 41 min 94,669,684,516,387,853
-## ----------------------------------------------
-## 4,210 1 hr 46 min 396,334,245,663,786,197
-## 4,220 2 hr 36 min 686,315,421,273,233,617
-## ----------------------------------------------
-## 4,229 3 hr 59 min 1,276,812,967,623,946,997
-## 4,230 7 hr 48 min 3,396,334,245,663,786,197
-## 4,231 7 hr 57 min 3,484,957,213,536,676,883
-## 4,232 9 hr 46 min 4,686,798,799,354,632,647
+## 20 0.0001 sec 197
+## 100 0.0005 sec 5,167
+## -----------------------------------------------
+## 500 0.0045 sec 543,853
+## 1,000 0.0173 sec 8,391,283
+## 1,500 0.0477 sec 79,962,683
+## 2,000 0.1246 sec 736,275,167
+## 2,500 0.3298 sec 6,946,986,197
+## 3,000 0.9487 sec 75,315,729,173
+## 3,500 3.9416 sec 1,837,839,918,353
+## 4,000 62.3637 sec 313,231,816,543,853
+## -----------------------------------------------
+## 4,100 3 min 54 sec 2,696,154,867,812,347
+## 4,200 41 min 94,669,684,516,387,853
+## -----------------------------------------------
+## 4,210 1 hr 46 min 396,334,245,663,786,197
+## 4,220 2 hr 36 min 686,315,421,273,233,617
+## -----------------------------------------------
+## 4,229 3 hr 59 min 1,276,812,967,623,946,997
+## 4,230 7 hr 48 min 3,396,334,245,663,786,197
+## 4,231 7 hr 57 min 3,484,957,213,536,676,883
+## 4,232 9 hr 46 min 4,686,798,799,354,632,647
+## 4,233 10 hr 46 min 5,396,334,245,663,786,197
+## 4,234 11 hr 48 min 6,165,678,739,293,946,997
+## 4,235 11 hr 57 min 6,276,812,967,623,946,997
+## 4,236 12 hr 00 min 6,312,646,216,567,629,137
+## 4,237 12 hr 13 min 6,484,957,213,536,676,883
+## 4,238 12 hr 48 min 6,918,997,653,319,693,967
+## 4,239 14 hr 8 min 7,986,315,421,273,233,617
+## 4,240 15 hr 15 min 8,918,997,653,319,693,967
+## 4,241 15 hr 18 min 8,963,315,421,273,233,617
## Length # of length # cumulative
## 1 4 4
@@ -124,4 +133,9 @@ while(1) {
## 16 72 4162
## 17 42 4204
## 18 24 4228
-## 19
+## 19 13 4241
+## 20
+## 21
+## 22
+## 23
+## 24
diff --git a/challenge-147/james-smith/perl/ch-2.pl b/challenge-147/james-smith/perl/ch-2.pl
index 76bed977ae..632d5ac1e1 100644
--- a/challenge-147/james-smith/perl/ch-2.pl
+++ b/challenge-147/james-smith/perl/ch-2.pl
@@ -6,16 +6,12 @@ use warnings;
use feature qw(say);
my %q = map { $_=>$_ } (my @p = (0,1));
-
for(my $o=2;;$o++) {
- $q{$p[$o] ||= $o*(3*$o-1)/2}||=$o;
- for(my $i=1, ($q{$p[$o]||=$o*(3*$o-1)/2}||=$o);$i<$o;$i++) {
- next unless my $t = $q{$p[$o]-$p[$i]};
+ for(my $i=1, ($q{$p[$o]||=$o*(3*$o-1)/2}||=$o); $i<$o; $i++) {
+ (my $t=$q{$p[$o]-$p[$i]}) || next;
(push(@p, @p * (3*@p-1)/2), $q{$p[-1]}=@p) while $p[$o]+$p[$i] > $p[-1];
- next unless my $s = $q{$p[$o]+$p[$i]};
- say "First 2 pents are: p($o) = $p[$o], p($i) = $p[$i], p($o) + p($i) = ",
- $p[$o]+$p[$i]," = p($s), p($o) - p($i) = ",$p[$o]-$p[$i]," = p($t)";
- exit;
+ (my $s=$q{$p[$o]+$p[$i]}) || next;
+ die "First 2 pents are:\n p($o) = $p[$o]\n p($i) = $p[$i],\n p($o) + p($i) = $p[$s] = p($s),\n p($o) - p($i) = $p[$t] = p($t).\n"
}
}