Skip to content

Instantly share code, notes, and snippets.

@vayn
Created May 22, 2010 07:45
Show Gist options
  • Save vayn/409891 to your computer and use it in GitHub Desktop.
Save vayn/409891 to your computer and use it in GitHub Desktop.
sieve_of_eratosthenes
<?php
define('MAX_NUM', 1000000);
$all = array_fill(0, MAX_NUM, 0);
$i = 2;
if ($all[$i] == 0) {
echo $i . "\n"; //测试性能时去掉这行。输出会占据大部分时间。
for ($j = $i; $j < MAX_NUM; $j+=$i) {
$all[$j] = 1;
}
}
for ($i = 3; $i < MAX_NUM; $i+=2) {
if ($all[$i] == 0) {
echo $i . "\n"; //测试性能时去掉这行。输出会占据大部分时间。
for ($j = $i; $j < MAX_NUM; $j+=$i) {
$all[$j] = 1;
}
}
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment