p4raw-id: //depot/perl@8128
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;
}
}
push @INC, '../lib';
}
-print "1..16\n";
+print "1..20\n";
use strict;
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";
+