<?php
function prime($max) {
$data[0] = 2;
$total = 1;
for ($i = 3; $i <= $max; $i += 2) {
$flag = true;
$sqrt = ceil(sqrt($i)); //除法求质数
for ($j = 0; $j < $total && $data[$j] <= $sqrt; $j ++) {
if (0 === $i % $data[$j]) {
$flag = false;
break;
}
}
if ($flag)
$data[$total++] = $i;
}
return $data;
}