Project Euler Solutions by Ross Marks

<?php
/*****************************
 * ProjectEuler - Problem 7
 * By Ross Marks
 *****************************
 * 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?
 ****************************/

function get_prime($nth$t false$tCheck 1000){
    
$singular = !is_array($nth);
    
$nth array_filter((array) $nth,
        function(
$n) {
            return 
is_int($n) && $n 0;
        });
    if (!
$nth) return $singular null : array();
    
$n max($nth);
    
$primes = array(=> 2);
    if (
$n == 1) {
        return 
$singular $primes[1] : $primes;
    }
    
$c 1;
    
$p 3;
    
$begin microtime(true);
    while (
true){
        
$prime true;
        
$sqrt sqrt($p);
        for (
$i 1$i $c && $primes[$i] <= $sqrt$i++) {
            if (
$p $primes[$i] == 0) {
                
$prime false;
                break;
            }
        }
        if (
$prime) {
            
$primes[++$c] = $p;
            if (
$c == $n) {
                break;
            }
        }
        if (
$t && ($p $tCheck <= 1) && (microtime(true) - $begin) > $t) {
            break;
        }
        
$p += 2;
    }
    
    if (
$singular) {
        return isset(
$primes[$n]) ? $primes[$n] : null;
    } else {
        return 
array_intersect_key($primesarray_fill_keys($nthnull));
    }
}

$awns get_prime(10001);
echo 
"Awnser: ".$awns."\n";
?>