Created
December 31, 2015 08:11
-
-
Save KerryJones/a0a0e985b1958a7d08d6 to your computer and use it in GitHub Desktop.
PHP: Sieve of Eratosthenes
This file contains hidden or 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 | |
function sieve_of_eratosthenes($max) { | |
$flags = array_fill(0, $max, true); | |
$flags[0] = $flags[1] = false; | |
foreach ( $flags as $index => &$is_prime ) { | |
if ( $is_prime ) { | |
echo $index . "<br>\n"; | |
for( $i = $index*$index; $i < $max; $i +=$index ) | |
$flags[$i] = false; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment