Project Euler Problem 7 (PHP)

Share the joy
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  

Problem 7

By listing the first six prime numbers: 2, 3, 5, 7, 11, and 13, we can see that the 6th prime is 13.

What is the 10,001st prime number?

Script

Execution Time: 1.3918 seconds

function isPrime($n)
{
    $m = floor(sqrt($n));
    for ($x=2; $x<=$m; $x++) {
        if ($n%$x == 0) {
            return false;
        }
    }

    return true;
}

$n = 3;
$k = 2;
$kMax = 10001;

while ($k <= $kMax) {
    if (isPrime($n)) {
        if ($k == $kMax) {
            echo $n.PHP_EOL;
            break;
        }
        $k++;
    }
    $n=$n+2;
}

Share the joy
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
This entry was posted in PHP, Programming, Project Euler and tagged , , , . Bookmark the permalink.

Comments are closed.