summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJarkko Hietaniemi <jhi@iki.fi>2000-12-15 15:32:22 +0000
committerJarkko Hietaniemi <jhi@iki.fi>2000-12-15 15:32:22 +0000
commit1725693fac4322554ed5d17f384f2502ef67bf23 (patch)
tree0127e8a978e18c11dd1f14627bce770b9b3ba369
parentd86ca223b7bea516175c99c61fc271cc462a2de7 (diff)
downloadperl-1725693fac4322554ed5d17f384f2502ef67bf23.tar.gz
Still buggy findgteprime, fix from Eric Joanis <joanis@cs.toronto.edu>.
p4raw-id: //depot/perl@8128
-rw-r--r--lib/Tie/SubstrHash.pm14
-rw-r--r--t/lib/tie-substrhash.t14
2 files changed, 21 insertions, 7 deletions
diff --git a/lib/Tie/SubstrHash.pm b/lib/Tie/SubstrHash.pm
index 3b92bd1cee..afe5d8dc25 100644
--- a/lib/Tie/SubstrHash.pm
+++ b/lib/Tie/SubstrHash.pm
@@ -186,19 +186,21 @@ sub ceil {
sub findgteprime { # find the smallest prime integer greater than or equal to
use integer;
+# It may be sufficient (and more efficient, IF IT IS CORRECT) to use
+# $max = 1 + int sqrt $num and calculate it once only, but is it correct?
+
my $num = ceil(shift);
return 2 if $num <= 2;
$num++ unless $num % 2;
- my $max = int sqrt $num;
-
NUM:
for (;; $num += 2) {
- for ($i = 3; $i <= $max; $i += 2) {
- next NUM unless $num % $i;
- }
- return $num;
+ my $max = int sqrt $num;
+ for ($i = 3; $i <= $max; $i += 2) {
+ next NUM unless $num % $i;
+ }
+ return $num;
}
}
diff --git a/t/lib/tie-substrhash.t b/t/lib/tie-substrhash.t
index d21ca0ab1b..8256db7b58 100644
--- a/t/lib/tie-substrhash.t
+++ b/t/lib/tie-substrhash.t
@@ -7,7 +7,7 @@ BEGIN {
push @INC, '../lib';
}
-print "1..16\n";
+print "1..20\n";
use strict;
@@ -97,3 +97,15 @@ print "ok 15\n";
print "not " unless Tie::SubstrHash::findgteprime(13.000001) == 17;
print "ok 16\n";
+print "not " unless Tie::SubstrHash::findgteprime(114) == 127;
+print "ok 17\n";
+
+print "not " unless Tie::SubstrHash::findgteprime(1000) == 1009;
+print "ok 18\n";
+
+print "not " unless Tie::SubstrHash::findgteprime(1024) == 1031;
+print "ok 19\n";
+
+print "not " unless Tie::SubstrHash::findgteprime(10000) == 10007;
+print "ok 20\n";
+