Skip to content

Instantly share code, notes, and snippets.

@timw4mail
Created October 19, 2011 14:55
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save timw4mail/1298529 to your computer and use it in GitHub Desktop.
Save timw4mail/1298529 to your computer and use it in GitHub Desktop.
PHP prime number generator
<!DOCTYPE html>
<html>
<head>
<title>Prime Number Generator</title>
</head>
<body onload="location.href='#bottom'">
<?php
$start = microtime(TRUE);
$primes = json_decode(file_get_contents("prime_array.json"));
$i = $primes[count($primes) -1] + 1;
$ip = $i + 999;
if($i % 2 === 0)
{
++$i;
++$ip;
}
for($i;$i < $ip;$i+=2)
{
$prime = TRUE;
foreach($primes as $p)
{
//Divisible by another prime…it's a composite
if($i % $p === 0)
{
$prime = FALSE;
break;
}
}
if($prime === TRUE)
{
$primes[] = $i;
}
}
echo implode(", ", $primes);
file_put_contents("prime_array.json", json_encode($primes));
$end = microtime(TRUE);
?><p> Generated <?= count($primes) ?> primes in <?= $end - $start ?> seconds</p>
<span id="bottom"></span>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment