Created
May 22, 2010 07:45
-
-
Save vayn/409891 to your computer and use it in GitHub Desktop.
sieve_of_eratosthenes
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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