Skip to content

Embed URL

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
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
Something went wrong with that request. Please try again.