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; }
转载自http://www.phpchina.com/index.php?action-viewthread-tid-5728