Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- function find_nth_prime($x = 6) {
- // Find the $xth prime.
- $i = 3;
- $a = $b = 1;
- while ($i <= $x) {
- if ($b == 1) {
- $r = 6 * $a - 1;
- $b = 0;
- } else {
- $r = 6 * $a + 1;
- $b = 1;
- $a = $a + 1;
- }
- $c = pow($r, .5);
- $d = 1;
- $e = 3;
- while ($e <= $c) {
- if ($r % $e == 0) {
- $d = 0;
- }
- $e = $e + 2;
- }
- if ($d == 1) {
- $i = $i + 1;
- }
- }
- return $r;
- }
- echo find_nth_prime(10001); // 6.683 seconds
- ?>
Add Comment
Please, Sign In to add comment