Created
April 22, 2016 21:22
-
-
Save zgurya/f495a6a4f68d9fbdc43fbcf0b9191293 to your computer and use it in GitHub Desktop.
Search circular prime in range from 2 to 1000000
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
$circular_primes=array(); | |
function is_prime ($num){ | |
for ($x = 2; $x <= sqrt($num); $x++) | |
if (!($num % $x)) return false; | |
return true; | |
} | |
function is_circular($str){ | |
$tmp_array=array(); | |
array_push($tmp_array,$str); | |
$str_split=str_split($str); | |
for($i=1;$i<count($str_split);$i++){ | |
$str_split_first=array_shift($str_split); | |
array_push($str_split,$str_split_first); | |
$d=implode("", $str_split); | |
if(is_prime($d)) array_push($tmp_array,$d); | |
} | |
if(count(str_split($str))==count($tmp_array)){ | |
return true; | |
}else{ | |
return false; | |
} | |
} | |
for($i=2;$i<=1000000;$i++){ | |
if(is_prime($i)){ | |
if(is_circular($i)){ | |
if (!in_array($i, $circular_primes)) { | |
array_push($circular_primes,$i); | |
echo $i.'</br>'; | |
} | |
} | |
} | |
} | |
echo 'Count of circular prime is '.count($circular_primes); | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment