试除法求质数的最快方法!

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



原文地址:https://www.cnblogs.com/caizhendong/p/5456045.html