return $num;
}
+# See:
+#
+# http://www-groups.dcs.st-andrews.ac.uk/~history/HistTopics/Prime_numbers.html
+#
+
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 $i;
+ my $sqrtnum = int sqrt $num;
+ my $sqrtnumsquared = $sqrtnum * $sqrtnum;
NUM:
for (;; $num += 2) {
- my $max = int sqrt $num;
- for ($i = 3; $i <= $max; $i += 2) {
+ if ($sqrtnumsquared < $num) {
+ $sqrtnum++;
+ $sqrtnumsquared = $sqrtnum * $sqrtnum;
+ }
+ for ($i = 3; $i <= $sqrtnum; $i += 2) {
next NUM unless $num % $i;
}
return $num;