Solve ProjectEuler 049 and 050.

This commit is contained in:
Jens True 2024-07-02 07:46:37 +00:00
parent 8d60e1b905
commit 3903a79785
2 changed files with 69 additions and 0 deletions

@ -0,0 +1,30 @@
<?php
include("vendor/autoload.php");
for($p1 = 1000; $p1<9999-(2*3330); $p1++)
{
if(CodingTests\Prime::isPrime($p1))
{
$p2 = $p1 + 3330;
if(CodingTests\Prime::isPrime($p2))
{
$p3 = $p2 + 3330;
if(CodingTests\Prime::isPrime($p3))
{
$p1_occ = count_chars($p1);
ksort($p1_occ);
$p2_occ = count_chars($p2);
ksort($p2_occ);
$p3_occ = count_chars($p3);
ksort($p3_occ);
if( $p1_occ == $p2_occ && $p2_occ == $p3_occ)
{
echo "Permutation $p1$p2$p3\n";
}
}
}
}
}

@ -0,0 +1,39 @@
<?php
include("vendor/autoload.php");
define("MAX", 1000000);
CodingTests\Prime::isPrime((3));
$primes = [];
function is_prime(int $n) :bool{for($i=$n**.5|1;$i&&$n%$i--;);return!$i&&$n>1;}
for($p= 1; $p < MAX; $p+=2)
{
if($p%1001 == 0)
{
echo $p."\n";
}
if(CodingTests\Prime::isPrime($p))
{
$primes[] = $p;
}
}
echo "Precalculate done";
$longest = 0;
for($start = 0; $start < sizeof($primes); $start++) {
for($len = 1; $len <sizeof($primes)-$start; $len++)
{
//print_r(array_slice($primes, $start, $len));
$sum = array_sum(array_slice($primes, $start, $len));
if($sum > MAX)
{
continue 2;
}
if(in_array($sum, $primes) && $len > $longest)
{
echo "Found $len=>$sum \n";
$longest = $len;
}
}
}