素数生成器的算法【PHP EDITION】
<?phpfunction CalcPrimes($intPrimeUBound)
{
//intPrimeUBound 是计算素数的上限
for($i = 0; $i <= $intPrimeUBound; $i++)
{
$bitArray[$i] = 1;
}
for($i = 2; $i <= (int)sqrt($intPrimeUBound); $i++)
{
if(1 == $bitArray[$i])
{
for($j = $i; $j * $i <= $intPrimeUBound; $j++)
{
$bitArray[$i * $j] = 0;
}
}
}
for($i = 2; $i <= $intPrimeUBound; $i++)
{
if(1 == $bitArray[$i])
$Primes[] = $i;
}
return $Primes;
}
?>
页:
[1]