Project Euler Solutions by Ross Marks

<?php
/*****************************
 * ProjectEuler - Problem 35
 * By Ross Marks
 *****************************
 * The number, 197, is called a circular prime because all rotations of the digits: 197, 971, and 719, are themselves prime.
 * 
 * There are thirteen such primes below 100: 2, 3, 5, 7, 11, 13, 17, 31, 37, 71, 73, 79, and 97.
 * 
 * How many circular primes are there below one million?
 ****************************/
 
include('_functions.php');

$awns 0;
$primes array_fill(01000000'0');

// setup main array with all primes as 2
for($i 1$i<=1000000$i++){
    if(
isPrime($i)){
        
$primes[$i] = '2';
    }
}

// check rotations of prime
foreach($primes as $key => $val){
    if(
$val == '2'){

        
$rotation_array = array();

        
// populate rotation array with all rotations
        
for($i 0$i strlen((string)$key); $i++){
            
$rotation_array[str_shift((string)$key, -$i)] = 1;
        }

        
// check all rotations
        
$fine 1;
        foreach(
$rotation_array as $key2 => $val2){
            if(@
$primes[$key2] != '2'){
                
$fine 0;
            }
        }

        
// if all rotations are primes then fix main array
        
if($fine == 1){
            foreach(
$rotation_array as $key2 => $val2){
                
$primes[$key2] = '1';
            }
        }
        
    }
}

foreach(
$primes as $key => $val){
    if(
$val == '1')
        
$awns++;
}


echo 
"Answer: $awns";



function 
str_shift($str$len) {
    
$len $len strlen($str);
    return 
substr($str$len) . substr($str0$len);
}

?>