40 lines
849 B
PHP
40 lines
849 B
PHP
|
<?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;
|
||
|
}
|
||
|
}
|
||
|
}
|