Restructuring

This commit is contained in:
2024-07-01 13:49:44 +00:00
parent f11b705ef0
commit 8d60e1b905
194 changed files with 1296 additions and 112 deletions

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,61 @@
<?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";