[libmath-prime-util-perl] 31/54: Revised example of Math::Factor::XS::matches

Partha P. Mukherjee ppm-guest at moszumanska.debian.org
Thu May 21 18:52:09 UTC 2015


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 301c9a045cbd664b9ca6414ed9acfe9b893069f5
Author: Dana Jacobsen <dana at acm.org>
Date:   Fri Feb 14 15:00:45 2014 -0800

    Revised example of Math::Factor::XS::matches
---
 examples/project_euler_342.pl |  4 ++--
 lib/Math/Prime/Util.pm        | 24 +++++++++++++++++-------
 2 files changed, 19 insertions(+), 9 deletions(-)

diff --git a/examples/project_euler_342.pl b/examples/project_euler_342.pl
index 42051f9..698207c 100644
--- a/examples/project_euler_342.pl
+++ b/examples/project_euler_342.pl
@@ -20,8 +20,8 @@ use Math::GMPz;
 # TIMING:
 #              10^7    2*10^7   10^8     10^10
 # Clever       0.07s    0.09s   0.24s    5s
-# Brute        5.8s    11.0s   53.9s     5 hours
-# Simple MPU  13.9s    29.7s  192s       1-2 days?
+# Brute        5.7s    11.0s   53.9s     5 hours
+# Simple MPU  12.0s    27.3s  174s       1-2 days?
 # Simple Pari 13.6s    33.4s  277s       5 days?
 #   
 
diff --git a/lib/Math/Prime/Util.pm b/lib/Math/Prime/Util.pm
index 51519b7..69fe772 100644
--- a/lib/Math/Prime/Util.pm
+++ b/lib/Math/Prime/Util.pm
@@ -2800,13 +2800,24 @@ command line.  This is faster than Mathematica until C<10^13>.
   } int(sqrt($limit));
   say $sum;
 
-Produce the C<matches> result from L<Math::Factor::XS> without skipping:
+To get the result of L<Math::Factor::XS/matches>:
 
   use Math::Prime::Util qw/divisors/;
-  use Algorithm::Combinatorics qw/combinations_with_repetition/;
+  sub matches {
+    my @d = divisors(shift);
+    return map { [$d[$_],$d[$#d-$_]] } 1..(@d-1)>>1;
+  }
   my $n = 139650;
-  my @matches = grep { $_->[0] * $_->[1] == $n && $_->[0] > 1 }
-                combinations_with_repetition( [divisors($n)], 2 );
+  say "$n = ", join(" = ", map { "$_->[0]·$_->[1]" } matches($n));
+
+or its C<matches> function with the C<skip_multiples> option:
+
+  sub matches {
+    my @d = divisors(shift);
+    return map { [$d[$_],$d[$#d-$_]] }
+           grep { my $div=$d[$_]; !scalar(grep {!($div % $d[$_])} 1..$_-1) }
+           1..(@d-1)>>1; }
+  }
 
 Compute L<OEIS A054903|http://oeis.org/A054903> just like CRG4's Pari example:
 
@@ -2992,8 +3003,7 @@ not support bigints.  Both are implemented with trial division, meaning they
 are very fast for really small values, but quickly become unusably slow
 (factoring 19 digit semiprimes is over 700 times slower).  The function
 C<count_prime_factors> can be done in MPU using C<scalar factor($n)>.
-MPU has no equivalent to C<matches>, but see the L</"EXAMPLES"> section
-for a way to produce the results.
+See the L</"EXAMPLES"> section for a 2-line function replicating C<matches>.
 
 L<Math::Big> version 1.12 includes C<primes> functionality.  The current
 code is only usable for very tiny inputs as it is incredibly slow and uses
@@ -3316,7 +3326,7 @@ native size integers.  With bigints we finally see it work well.
 
 =item *
 
-Math::Pari built with 2.3.5 not only has a better primality test vs.
+Math::Pari built with 2.3.5 not only has a better primality test versus
 the default 2.1.7, but runs faster.  It still has quite a bit of overhead
 with native size integers.  Pari/GP 2.5.0 takes 11.3s, 16.9s, and 2.9s
 respectively for the tests above.  MPU is still faster, but clearly the

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



More information about the Pkg-perl-cvs-commits mailing list