Skip to content

Instantly share code, notes, and snippets.

@KerryJones
Created December 31, 2015 08:11
Show Gist options
  • Save KerryJones/a0a0e985b1958a7d08d6 to your computer and use it in GitHub Desktop.
Save KerryJones/a0a0e985b1958a7d08d6 to your computer and use it in GitHub Desktop.
PHP: Sieve of Eratosthenes
<?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