Created
February 2, 2011 04:19
-
-
Save twaddington/807227 to your computer and use it in GitHub Desktop.
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 isPrime($number) { | |
// A prime number is only divisible by 1 and itself. | |
// All numbers are divisible by 1, so start with 2. | |
for ($div = 2; $div < $number; $div++) { | |
// If $number is wholly divisible by $div (no remainder). | |
if (($number % $div) === 0) { | |
// If we can evenly divide $number by anything that | |
// is not 1 or itself, it is not prime and we can return | |
// immediately. | |
return false; | |
} | |
} | |
// Return true | |
return true; | |
} | |
$i=2; | |
while ($i<=1000) { | |
if (isPrime($i)) { | |
echo "Prime: " . $i . "\n"; | |
} | |
$i++; | |
} | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
It is obvious that no prime number is divisible by 2, so you would speed this up by