# [libmath-prime-util-perl] 14/54: Update PE 357 example

This is an automated email from the git hooks/post-receive script.

ppm-guest pushed a commit to annotated tag v0.38
in repository libmath-prime-util-perl.

commit cc4ccdf648ac83a0efd8fb09622e0dcf398d9fb2
Author: Dana Jacobsen <d...@acm.org>
Date:   Fri Jan 31 22:27:38 2014 -0800

Update PE 357 example
---
examples/project_euler_357.pl | 26 +++++++++++++++++++-------
1 file changed, 19 insertions(+), 7 deletions(-)

diff --git a/examples/project_euler_357.pl b/examples/project_euler_357.pl
index b805d15..501c90f 100644
--- a/examples/project_euler_357.pl
+++ b/examples/project_euler_357.pl
@@ -6,21 +6,33 @@ use List::MoreUtils qw/all/;

my \$maxn = shift || 100_000_000;

-# Takes ~ 5 seconds
-my \$sum = 0;
+prime_precalc(\$maxn);    # Speeds up is_prime, but not necessary
+my(\$sum, \$n) = (1, 0);
forprimes {
-  my \$n = \$_-1;                  # 1+\$n/1 is prime  (hence n=1 or even)
-  if (is_prime(2+(\$n>>1))) {     # 2+\$n/2 is prime  (noting n is even or 1)
+  \$n = 2*\$_ - 4;                 # 2+\$n/2 is prime
+  if (is_prime(\$n+1)) {          # 1+\$n/1 is prime
if (moebius(\$n) != 0) {      # n should be square free
\$sum += \$n if all { is_prime(\$_+\$n/\$_) } divisors(\$n);
}
}
-} \$maxn;
+} int(\$maxn/2);
print "\$sum\n";

+
+# This version is a little more direct.
+#  my(\$sum, \$n) = (0, 0);
+#  forprimes {
+#    \$n = \$_-1;                     # 1+\$n/1 is prime  (hence n=1 or even)
+#    if (is_prime(2+(\$n>>1))) {     # 2+\$n/2 is prime  (noting n is even or 1)
+#      if (moebius(\$n) != 0) {      # n should be square free
+#        \$sum += \$n if all { is_prime(\$_+\$n/\$_) } divisors(\$n);
+#      }
+#    }
+#  } \$maxn;
+#  print "\$sum\n";
+
+
# We could additionally check these:
#   if ( ((\$n+2) % 4) == 0 || \$n == 1) {
# Using all is more efficient, but this works:
#   \$sum += \$n unless scalar grep { !is_prime(\$_+\$n/\$_) } divisors(\$n);
-# We could alternately generate primes to \$maxn/2,
-#    my \$n = 2*\$_-4; if (is_prime(\$n+1)) { ... }

--
Alioth's /usr/local/bin/git-commit-notice on
/srv/git.debian.org/git/pkg-perl/packages/libmath-prime-util-perl.git

_______________________________________________
Pkg-perl-cvs-commits mailing list
Pkg-perl-cvs-commits@lists.alioth.debian.org
http://lists.alioth.debian.org/cgi-bin/mailman/listinfo/pkg-perl-cvs-commits