Coder Profile - Show off your skills, get a coder profile.
 
 
 
code pin board What is the 10001st prime number? Download Source Code
Author Details Code Information
perito

Pinned 7 Codes
Posted 0 Coding Articles

Send A Message
View Coders Profile
Language PHP (Hypertext Preprocessor)
Expires Never
Length 470 Characters (31 Lines)
Password no password
  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. ?>
code pin board Back To Code Pin Board Post New Code
Please login to post comments.
Page 1 of 1
 
 
Part of the MyPingle Network
Development Blog :: Make A Donation :: Contact Me
Terms & Conditions :: Privacy Policy :: Documents
Version 1.44.00
Copyright © 2007 - 2008, Scott Thompson, All Rights Reserved