title: PHP Faster Prime Number generator
body: <p>hey guys I'm trying to squeeze more primes out of my generator but have hit a limit of +- 14,000,000 or so primes in a sixty second run, i'm looking to push that possibly up to 25-30 mil if possible. Any ideas on how to accomplish this feat?</p>
  
  <p>Here's my php code</p>
  
  <pre><code>&lt;?php
  $i = 2;
  $primes = array();
  while(true)
  {
      $prime=true;
      $sqrt=floor(sqrt($i));
      foreach($primes as $num)
      {
          if($i%$num==0)
          {
              $prime=false;
              break;
          }
          if($num&gt;$sqrt) break;
      }
      if($prime) echo "$i\n";
      $i++;
  }
  </code></pre>
  
  <p>And here's the little bash script to run it</p>
  
  <pre><code>#!/bin/bash
  outfile="$1.out"
  `php $1 &gt; $outfile &amp;`
  sleep 60
  killall php
  </code></pre>
  
  <p><strong>EDIT</strong></p>
  
  <p>Here's a pcntl_forked version from last night just to see what would be faster.
  For some weird reason, the less forking I did the faster if was, I found out the
  bottlenecking was coming from <code>gmp_strval(gmp_nextprime($start))</code> which is extremely slow.</p>
  
  <pre><code>&lt;?php
  $primeCount = 0;
  for ($i = 1; $i &lt;= 1; ++$i) {
      $start = $i;
      $pid = pcntl_fork();
  
      if (!$pid) {
          while(true) echo $start = gmp_strval(gmp_nextprime($start)) . "\n";
          exit($i);
      }
  }
  
  while (pcntl_waitpid(0, $status) != -1) {
      $status = pcntl_wexitstatus($status);
      #echo "Child $status , $primeCount completed\n";
  }
  die;
  </code></pre>