Skip to content

Instantly share code, notes, and snippets.

@timw4mail
Created September 17, 2012 20:18
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/3739533 to your computer and use it in GitHub Desktop.
Save timw4mail/3739533 to your computer and use it in GitHub Desktop.
Generating prime numbers
<?php
set_time_limit(-1);
$primes = json_decode(file_get_contents("prime.json"));
while (TRUE)
{
$i = $primes[count($primes) -1] + 1;
$ip = $i + 999;
$num_primes = count($primes);
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;
$num_primes++;
}
}
file_put_contents("prime.json", json_encode($primes));
echo "Generated {$num_primes} primes \n";
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment