Problem ProjectEuler 044
This commit is contained in:
32
ProjectEuler/044/solve.php
Normal file
32
ProjectEuler/044/solve.php
Normal file
@ -0,0 +1,32 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
function Pn($n)
|
||||||
|
{
|
||||||
|
return $n * (3 * $n - 1) / 2;
|
||||||
|
}
|
||||||
|
|
||||||
|
$pn = [];
|
||||||
|
for ($i = 1; $i < 2500; $i++) {
|
||||||
|
$pn[] = (int) Pn($i);
|
||||||
|
}
|
||||||
|
$smallest_diff = PHP_INT_MAX;
|
||||||
|
echo "Precalculate done\n";
|
||||||
|
for ($ij = 0; $ij < sizeof($pn); $ij++) {
|
||||||
|
echo "$ij%\n";
|
||||||
|
$j = $pn[$ij];
|
||||||
|
for ($ik = $ij + 1; $ik < sizeof($pn); $ik++) {
|
||||||
|
$k = $pn[$ik];
|
||||||
|
$sum = $j + $k;
|
||||||
|
if (in_array($sum, $pn)) {
|
||||||
|
$diff = (int) abs($k - $j);
|
||||||
|
if (in_array($diff, $pn, true)) {
|
||||||
|
if ($diff < $smallest_diff) {
|
||||||
|
$smallest_diff = $diff;
|
||||||
|
echo "Found: Pn($ij)=$j Pn($ik)=$k Sum: $sum Diff: $diff\n";
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
echo "Smallest diff: $smallest_diff";
|
Reference in New Issue
Block a user