Prime Number Generator
From MFSP Wiki
The Prime Number Generator is a relatively simple construct: it goes from the first number input to the second number one input, checking if any numbers are prime between the two. If it is prime, it prints it. The script is a modified version of the fancyPrimality function found on the Primality Tester page.
<?php
function Primality ($num) {
if ($num < 2) {
return false;
}
if ($num == 2) {
return true;
}
if ($num % 2 == 0 && $num != 2) {
return false;
}
else {
$lim = intval(sqrt($num));
for ($i = 3; $i <= $lim; $i+=2) {
if ($num % $i == 0) {
return false;
$set = "set";
break;
}
}
if (!isset($set)) {
return true;
}
}
}
for($i = $start; $i <= $end; $i++) {
if(Primality($i)) {
echo $i."\n";
}
}
?>
As with all scripts here, if you re-distribute the code, I ask that you simply include the following comments beforehand:
# Script by Andre Kessler for MathForSmartyPants.com, some rights reserved. # See http://creativecommons.org/licenses/by-nc-sa/3.0/ for more info. # Please leave these comments in the script if you re-distribute them.
