Solved ProjectEuler/035

This commit is contained in:
FuryFire
2012-04-25 15:26:13 +02:00
parent 6cc3b0ad82
commit 62c2c8f47b
7 changed files with 93 additions and 2 deletions

11
ProjectEuler/035/desc.yml Normal file
View File

@ -0,0 +1,11 @@
title: How many circular primes are there below one million?
url: http://projecteuler.net/problem=35
desc: |
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?
solution: Bruteforce
solutions:

View File

@ -0,0 +1,40 @@
<?php
define('MAX',1000000);
function is_prime($prime) {
$sqrt = sqrt($prime);
for ($i = 3; $i <= $sqrt; $i+=2){
if ($prime%$i == 0) return false;
}
return true;
}
function fact($int){
static $facts = array(1, 1, 2, 6, 24, 120, 720, 5040, 40320, 362880);
return $facts[$int];
}
$primes = array(2);
$primes_ordered = array(2);
for($i=3;$i<MAX;$i+=2) {
if(is_prime($i)) {
//$primes[] = $i;
$temp = str_split($i);
sort($temp);
$primes_ordered[] = implode($temp);
}
}
echo "Found primes below 1mil\n";
$result = 0;
foreach($primes_ordered as $p) {
$counts = fact(strlen($p));
foreach($primes_ordered as $order) {
if($p == $order) {
$counts--;
}
}
if($counts == 0) {
//echo $p."\n";
$result++;
}
}
echo $result;