Created
September 17, 2012 20:18
-
-
Save timw4mail/3739533 to your computer and use it in GitHub Desktop.
Generating prime numbers
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 | |
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