aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAbigail <abigail@abigail.be>2020-10-14 16:47:41 +0200
committerAbigail <abigail@abigail.be>2020-10-14 16:47:41 +0200
commiteebd72d40733e49542dad4c0d9acf22063d69cf7 (patch)
tree43765757bac4cd9f4945d98e7de6030e7412ee80
parentbf27525b9e6f6e315b1d5d7bdefdb406b6e7ad69 (diff)
downloadperlweeklychallenge-club-eebd72d40733e49542dad4c0d9acf22063d69cf7.tar.gz
perlweeklychallenge-club-eebd72d40733e49542dad4c0d9acf22063d69cf7.tar.bz2
perlweeklychallenge-club-eebd72d40733e49542dad4c0d9acf22063d69cf7.zip
Another slight improvement.
When hunting for the next prime which divides $gcd, exploit the fact that, other than 2 and 3, all primes are of the form 6 * k +/-1, for some positive integer k.
-rw-r--r--challenge-082/abigail/perl/ch-1.pl15
1 files changed, 10 insertions, 5 deletions
diff --git a/challenge-082/abigail/perl/ch-1.pl b/challenge-082/abigail/perl/ch-1.pl
index 76ec111012..6788e1ae45 100644
--- a/challenge-082/abigail/perl/ch-1.pl
+++ b/challenge-082/abigail/perl/ch-1.pl
@@ -72,10 +72,11 @@ my $gcd = stein $M, $N;
# $f * $p ^ $i, 1 <= $i <= $t to @f. We can then also divide
# $gcd by $p ^ $t.
#
-# Note that in the loop below, we try *all* numbers starting from 2,
-# not just primes. But any composite number will have a prime factor
-# smaller than itself, and this will already be divided out of $gcd,
-# so for any composite number $n, $n will never divide $gcd evenly.
+# Note that in the loop below, ideally, we want to check only
+# prime numbers (any composite number will have a prime factor
+# smaller than itself). Since, except for 2 and 3, all prime
+# numbers are of the form 6 * k +/- 1, the loop below checks
+# 2, 3, and then numbers of the form 6 * k +/- 1.
#
# Also note that we stop if the current number is larger than the
# square of $gcd. This means that, at the end of the loop, $gcd may
@@ -112,7 +113,11 @@ while ($p ** 2 <= $gcd) {
push @factors => map {my $f = $_; map {$f * $p ** $_} 1 .. $t} @factors;
}
- $p ++;
+ $p += $p == 2 ? 1
+ : $p == 3 ? 2
+ : $p % 6 == 1 ? 4
+ : 2;
+
}
#