Removing invalid previous solutions

This commit is contained in:
Jens True 2024-06-26 13:23:30 +00:00
parent 27fd0bf2ea
commit 0fbf5ed250
4 changed files with 61 additions and 101 deletions

@ -1,61 +0,0 @@
<?php
function is_prime($prime) {
if($prime < 1)
return false;
if($prime == 1)
return true;
if($prime == 2)
return true;
$sqrt = sqrt($prime);
for ($i = 3; $i <= $sqrt; $i+=2){
if ($prime%$i == 0) return false;
}
return true;
}
if (($handle = fopen("primes1000000.txt", "r")) !== FALSE) {
while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) {
if ($data[1] >= 1000000)
break;
//If there's an even digit (or a 5) we can instantly exclude it as when rotated it will not not be a prime.
if (
strpos((string) $data[1], "0") !== false ||
strpos((string) $data[1], "2") !== false ||
strpos((string) $data[1], "4") !== false ||
strpos((string) $data[1], "5") !== false ||
strpos((string) $data[1], "6") !== false ||
strpos((string) $data[1], "8") !== false
) {
continue;
}
$primes[] = (int) $data[1];
}
fclose($handle);
}
echo "File read\n";
$count = 1+1; //2 and 5 are primes but not included
foreach($primes as $prime)
{
//echo "Now testing $prime\n";
$newprime = $prime;
for($rotate = 1; $rotate < strlen($prime); $rotate++)
{
$newprime = substr($newprime, -1, 1).substr($newprime, 0, -1);
//echo "New prime: $newprime\n";
if(!in_array($newprime, $primes))
{
continue 2;
}
}
$count++;
echo $prime.PHP_EOL;
}
echo "Total: $count\n";

@ -1,7 +1,13 @@
<?php
define('MAX',1000000);
function is_prime($prime) {
if($prime < 1)
return false;
if($prime == 1)
return true;
if($prime == 2)
return true;
$sqrt = sqrt($prime);
for ($i = 3; $i <= $sqrt; $i+=2){
if ($prime%$i == 0) return false;
@ -9,32 +15,47 @@ function is_prime($prime) {
return true;
}
function fact($int){
static $facts = array(1, 1, 2, 6, 24, 120, 720, 5040, 40320, 362880);
return $facts[$int];
if (($handle = fopen("primes1000000.txt", "r")) !== FALSE) {
while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) {
if ($data[1] >= 1000000)
break;
//If there's an even digit (or a 5) we can instantly exclude it as when rotated it will not not be a prime.
if (
strpos((string) $data[1], "0") !== false ||
strpos((string) $data[1], "2") !== false ||
strpos((string) $data[1], "4") !== false ||
strpos((string) $data[1], "5") !== false ||
strpos((string) $data[1], "6") !== false ||
strpos((string) $data[1], "8") !== false
) {
continue;
}
$primes[] = (int) $data[1];
}
fclose($handle);
}
$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 "File read\n";
$count = 1+1; //2 and 5 are primes but not included
foreach($primes as $prime)
{
//echo "Now testing $prime\n";
$newprime = $prime;
for($rotate = 1; $rotate < strlen($prime); $rotate++)
{
$newprime = substr($newprime, -1, 1).substr($newprime, 0, -1);
//echo "New prime: $newprime\n";
if(!in_array($newprime, $primes))
{
continue 2;
}
}
$count++;
echo $prime.PHP_EOL;
}
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;
echo "Total: $count\n";