Guest User

Untitled

a guest
Jun 20th, 2018
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.46 KB | None | 0 0
  1. <?php
  2. function find_nth_prime($x = 6) {
  3. // Find the $xth prime.
  4. $i = 3;
  5. $a = $b = 1;
  6. while ($i <= $x) {
  7. if ($b == 1) {
  8. $r = 6 * $a - 1;
  9. $b = 0;
  10. } else {
  11. $r = 6 * $a + 1;
  12. $b = 1;
  13. $a = $a + 1;
  14. }
  15. $c = pow($r, .5);
  16. $d = 1;
  17. $e = 3;
  18. while ($e <= $c) {
  19. if ($r % $e == 0) {
  20. $d = 0;
  21. }
  22. $e = $e + 2;
  23. }
  24. if ($d == 1) {
  25. $i = $i + 1;
  26. }
  27. }
  28. return $r;
  29. }
  30. echo find_nth_prime(10001); // 6.683 seconds
  31. ?>
Add Comment
Please, Sign In to add comment