[libmath-prime-util-perl] 24/35: Update with some more iterator examples
Partha P. Mukherjee
ppm-guest at moszumanska.debian.org
Thu May 21 18:50:04 UTC 2015
This is an automated email from the git hooks/post-receive script.
ppm-guest pushed a commit to annotated tag v0.33
in repository libmath-prime-util-perl.
commit ec94f133d3efb393b33c19ceb7cf61b752b57ad5
Author: Dana Jacobsen <dana at acm.org>
Date: Mon Nov 11 14:10:59 2013 -0800
Update with some more iterator examples
---
examples/twin_primes.pl | 41 ++++++++++++++++++++++++++++++++++-------
1 file changed, 34 insertions(+), 7 deletions(-)
diff --git a/examples/twin_primes.pl b/examples/twin_primes.pl
index ac903f1..362a737 100755
--- a/examples/twin_primes.pl
+++ b/examples/twin_primes.pl
@@ -3,17 +3,25 @@ use strict;
use warnings;
use Math::Prime::Util qw/-nobigint
- prime_iterator next_prime
- nth_prime_upper prime_precalc/;
+ prime_iterator prime_iterator_object
+ next_prime is_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:
-# 3m36s Math::NumSeq::TwinPrimes (Perl 5.19.2 with v61)
-# 1.4s this iterator
-# 7.9s this iterator without the precalc
+# 1.0s bin/primes.pl --twin 2 64764839
+# 1.4s get_twin_prime_iterator2
+# 2.3s get_twin_prime_iterator1
+# 4.1s get_twin_prime_iterator3
+# 6.1s get_twin_prime_iterator3 (object iterator)
+# 7.6s get_twin_prime_iterator2 without precalc
+# 8.4s get_twin_prime_iterator1 without precalc
+# 10.9s get_twin_prime_iterator3 without precalc
+# 13.1s get_twin_prime_iterator3 without precalc (object iterator)
+# 219.8s Math::NumSeq::TwinPrimes (Perl 5.19.4 with v66)
# This speeds things up, but isn't necessary.
my $estimate = 5000 + int( nth_prime_upper($count) * 1.4 * log($count) );
@@ -37,8 +45,8 @@ sub get_twin_prime_iterator1 {
# 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);
+ my $p = next_prime($start-1);
+ my $prev = next_prime($p);
return sub {
do {
($prev, $p) = ($p, next_prime($p))
@@ -46,6 +54,25 @@ sub get_twin_prime_iterator2 {
$prev;
};
}
+
+# Use Iterator::Simple
+#use Iterator::Simple qw/igrep/;
+#sub get_twin_prime_iterator3 {
+# my $start = shift || 2;
+# return igrep { is_prime($_+2) } prime_iterator($start);
+#}
+
+# Not very efficient, using object iterator and peek.
+sub get_twin_prime_iterator4 {
+ my $p = shift || 2;
+ my $it = Math::Prime::Util::prime_iterator_object($p);
+ $p = $it->value();
+ return sub {
+ $it->next() while $it->peek() - $it->value() != 2;
+ $it->iterate();
+ };
+}
+
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