= 1,000,000 // $HI <= 1,100,000 // Note pi(1.1M) - pi(1M) = 7216, so there are 7216 primes in the file and // then the array the primes are loaded into from the file. // See C. F. Gauss, Werke, Vol 2, 2nd ed (1876), pp. 438-443 // https://books.google.com/books?id=BTun-k0U128C&pg=PA438#v=onepage&q&f=false //------------------------------------------------------------------------ // Load array from file of primes $primes = file("primes-1M-1.1M.txt"); // For all the primes beween 1,000,000 and 1,100,000. //$LO = 1000000; //$HI = 1100000; // For the myriad (block of 10,000) above 1,000,000. $LO = 1000000; $HI = 1010000; // The 7216 primes between 1M and 1.1M now loaded into array $primes[]. // $bucket[$k] will hold the number of centuries with $k primes. $buckets = array(); // 50 is the absolute maximum number of primes in a century. $MAX_BUCKETS = 50; // Init all buckets to zero. for ($i = 0; $i < $MAX_BUCKETS; $i++) $buckets[] = 0; $lim = $LO + 100; $k = 0; // Go through primes calculating how many are in each century, // increment $buckets array accordingly. foreach ($primes as $prime) { // Ignore primes out of range. if ($prime < $LO) continue; if ($prime > $HI) break; if ($prime < $lim) { $k++; } else { $buckets[$k]++; $k = 1; $lim += 100; } // else } // foreach // Get the last block. $buckets[$k]++; // Delete buckets at the end that still contain 0. $j = $MAX_BUCKETS - 1; do { if ($buckets[$j] == 0) unset($buckets[$j]); $j--; } while ($buckets[$j] == 0); print_r($buckets); ?>