[libmath-prime-util-perl] 12/72: Update a couple examples
Partha P. Mukherjee
ppm-guest at moszumanska.debian.org
Thu May 21 18:49:36 UTC 2015
This is an automated email from the git hooks/post-receive script.
ppm-guest pushed a commit to annotated tag v0.32
in repository libmath-prime-util-perl.
commit 25809f6a058a7d1a578b922018757bc81dbb359d
Author: Dana Jacobsen <dana at acm.org>
Date: Fri Aug 16 18:28:33 2013 -0700
Update a couple examples
---
examples/sophie_germain.pl | 38 +++++++++++++++++++++++++++-----------
examples/twin_primes.pl | 29 +++++++++++++++++++++++------
2 files changed, 50 insertions(+), 17 deletions(-)
diff --git a/examples/sophie_germain.pl b/examples/sophie_germain.pl
index 4588c7f..38c2f64 100755
--- a/examples/sophie_germain.pl
+++ b/examples/sophie_germain.pl
@@ -2,7 +2,9 @@
use strict;
use warnings;
-use Math::Prime::Util qw/-nobigint next_prime is_prime prime_iterator/;
+use Math::Prime::Util qw/-nobigint
+ prime_iterator is_prime
+ next_prime nth_prime_upper prime_precalc forprimes/;
my $count = shift || 20;
@@ -11,7 +13,22 @@ my $count = shift || 20;
# In this method, we add a filter in front of our iterator, to create a
# Sophie-Germain-prime iterator. This isn't the fastest way, but it's still
# 20x faster than Math::NumSeq::SophieGermainPrimes at 300k. If we add the
-# two-line precalc shown below, we can get another 4x or so more.
+# two-line precalc shown below, we can get another 4x or more.
+#
+# Example:
+# time perl examples/sophie_germain.pl 300000 | md5sum
+# d380d31256cc9bc54eb5f236b3edc16d -
+# 9.673s
+#
+# time perl -MMath::NumSeq::SophieGermainPrimes -E 'my $seq = Math::NumSeq::SophieGermainPrimes->new; do { say 0+($seq->next)[1] } for 1..300000' | md5sum
+# d380d31256cc9bc54eb5f236b3edc16d -
+# 4m11.5s
+#
+# With method 2:
+# time perl examples/sophie_germain.pl 300000 | md5sum
+# d380d31256cc9bc54eb5f236b3edc16d -
+# 1.828s
+
sub get_sophie_germain_iterator {
my $p = shift || 2;
@@ -26,18 +43,17 @@ for (1..$count) {
print $sgit->(), "\n";
}
-
# Method 2. At 300k this is 70x faster than Math::NumSeq::SophieGermainPrimes.
#
-# my $estimate = 100 + int( nth_prime_upper($count) * 1.6 * log($count) );
-# prime_precalc(2 * $estimate);
+#my $estimate = 100 + int( nth_prime_upper($count) * 1.6 * log($count) );
+#prime_precalc(2 * $estimate);
#
-# my $prime = 2;
-# for (1..$count) {
-# $prime = next_prime($prime) while (!is_prime(2*$prime+1));
-# print "$prime\n";
-# $prime = next_prime($prime);
-# }
+#my $prime = 2;
+#for (1..$count) {
+# $prime = next_prime($prime) while (!is_prime(2*$prime+1));
+# print "$prime\n";
+# $prime = next_prime($prime);
+#}
# Alternate method, 10-20% faster, would benefit from a tighter estimate.
#
diff --git a/examples/twin_primes.pl b/examples/twin_primes.pl
index 91e5372..ac903f1 100755
--- a/examples/twin_primes.pl
+++ b/examples/twin_primes.pl
@@ -2,22 +2,25 @@
use strict;
use warnings;
-use Math::Prime::Util qw/prime_iterator nth_prime_upper prime_precalc/;
+use Math::Prime::Util qw/-nobigint
+ prime_iterator next_prime
+ nth_prime_upper prime_precalc/;
my $count = shift || 20;
# Find twin primes (numbers where p and p+2 are prime)
# Time for the first 300k:
-# 3m28s Math::NumSeq::TwinPrimes
-# 2.5s this iterator
-# 9.1s this iterator without the precalc
+# 3m36s Math::NumSeq::TwinPrimes (Perl 5.19.2 with v61)
+# 1.4s this iterator
+# 7.9s this iterator without the precalc
# This speeds things up, but isn't necessary.
my $estimate = 5000 + int( nth_prime_upper($count) * 1.4 * log($count) );
prime_precalc($estimate);
-sub get_twin_prime_iterator {
+# Create a twin prime iterator using the prime_iterator construct
+sub get_twin_prime_iterator1 {
my $p = shift || 2;
my $it = prime_iterator($p);
my $prev = $it->(); # prev = 2
@@ -29,7 +32,21 @@ sub get_twin_prime_iterator {
$prev;
};
}
-my $twinit = get_twin_prime_iterator();
+
+# Create a twin prime iterator using the next_prime function
+# A bit faster than the prime_iterator version.
+sub get_twin_prime_iterator2 {
+ my $start = shift || 2;
+ my $prev = next_prime($start-1);
+ my $p = next_prime($prev);
+ return sub {
+ do {
+ ($prev, $p) = ($p, next_prime($p))
+ } while ($p-$prev) != 2;
+ $prev;
+ };
+}
+my $twinit = get_twin_prime_iterator2();
for (1..$count) {
print $twinit->(), "\n";
}
--
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