Project Euler Solutions by Ross Marks

<?php
/*****************************
 * ProjectEuler - Problem 37
 * By Ross Marks
 *****************************
 * The number 3797 has an interesting property. Being prime itself, it is possible to continuously remove digits from left to right, and remain prime at each stage: 3797, 797, 97, and 7. Similarly we can work from right to left: 3797, 379, 37, and 3.
 * 
 * Find the sum of the only eleven primes that are both truncatable from left to right and right to left.
 * 
 * NOTE: 2, 3, 5, and 7 are not considered to be truncatable primes.
 ****************************/
 
include('_functions.php');

$awns 0;
$primes = array();
$primes_count 0;

echo 
"[+] generating primes\n";
$generated_primes generatePrime(1000000);
unset(
$generated_primes[0]); // these are 1 digit primes
unset($generated_primes[1]);
unset(
$generated_primes[2]);
unset(
$generated_primes[3]);
echo 
"[+] locating truncables\n";

$count 0;
foreach(
$generated_primes as $no_test){

    
$no = (string)$no_test;
    
$len strlen($no)-1;

    
$fullprime 1;
    for(
$i 1$i <= $len+1$i++){
        
$small substr($no0$i);
        if(!
isPrime($small)){
            
$fullprime 0;
            break;
        }
    }

    if(
$fullprime == 1){
        
$no2 $no;
        for(
$i 1$i <= $len$i++){
            
$no2 substr($no21);
            if(!
isPrime($no2)){
                
$fullprime 0;
                break;
            }
        }
    }

    if(
$fullprime == 1){ // YEY found
        
$primes[] = $no;
        
$primes_count++;
        echo 
"[+] found $primes_count$no\n";
    }
    
$count++; // try next prime
}

foreach(
$primes as $prime){
    
$awns bcadd($awns$prime);
}
echo 
"[=] $awns ";


/***
 * ewwwwww
 */

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));
    }
}


function 
generatePrimes($n){ 
    
$prime = array();
    
$return_array = array();
 
    for(
$i 1$i <= $n$i++){ 
         
$prime[$i] = 0;
    } 
    
$k=2;
    
$mul 0;
    while(
$k $n){
        for(
$j $n >= $k*$j$j++){
            
$mul $j $k;
            
$prime[$mul]=1;
        }
        
$k++;
    }
    for(
$i 2$i <= $n$i++){
        if(
$prime[$i] == 0
            
$return_array[] = $i;
    } 
    return 
$return_array;
}
 

?>