Welcome to collectivesolver - Programming & Software Q&A with code examples. A website with trusted programming answers. All programs are tested and work.

Contact: aviboots(AT)netvision.net.il

Buy a domain name - Register cheap domain names from $0.99 - Namecheap

Scalable Hosting That Grows With You

Secure & Reliable Web Hosting, Free Domain, Free SSL, 1-Click WordPress Install, Expert 24/7 Support

Semrush - keyword research tool

Boost your online presence with premium web hosting and servers

Disclosure: My content contains affiliate links.

39,895 questions

51,826 answers

573 users

How to calculate print and save to file the first 21,000 prime numbers in PHP

1 Answer

0 votes
$n = 21000;
$prime = 2;
$numbers = 1;

$f = fopen("d:\\a_list_of_100000_prime_numbers.txt", "w") or die("Error open file!");

while ($numbers <= $n)
{
    for ($i = 2; $i <= $prime - 1; $i++)
    {
        if ($prime % $i == 0)
            break;
    }
    if ($i == $prime)
    {
        echo $prime . "<br />";
        fwrite($f, $prime . "\n");
        $numbers++;
    }
    $prime++;
}
fclose($f);
   
/*
run: 

...

236981
236983
236993
237011
237019
237043
237053
237067
237071
237073
237089
237091
237137
237143
237151
237157
237161
237163
237173
237179
237203

*/

 



answered May 14, 2016 by avibootz
...