From 1725693fac4322554ed5d17f384f2502ef67bf23 Mon Sep 17 00:00:00 2001 From: Jarkko Hietaniemi Date: Fri, 15 Dec 2000 15:32:22 +0000 Subject: [PATCH] Still buggy findgteprime, fix from Eric Joanis . p4raw-id: //depot/perl@8128 --- lib/Tie/SubstrHash.pm | 14 ++++++++------ t/lib/tie-substrhash.t | 14 +++++++++++++- 2 files changed, 21 insertions(+), 7 deletions(-) diff --git a/lib/Tie/SubstrHash.pm b/lib/Tie/SubstrHash.pm index 3b92bd1..afe5d8d 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 d21ca0a..8256db7 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"; + -- 2.7.4