Skip to content

Instantly share code, notes, and snippets.

@gavinsykes
Created April 22, 2019 20:10
Show Gist options
  • Save gavinsykes/857b3d58861c4b2951001cf72f3c8043 to your computer and use it in GitHub Desktop.
Save gavinsykes/857b3d58861c4b2951001cf72f3c8043 to your computer and use it in GitHub Desktop.
function euler_9($n) {
$a = $b = $c = 1;
for ($a = 1;$a <= $n-2;$a++) {
for ($b = 1; $b <= $n-1-$a;$b++) {
$c = $n - $a - $b;
if (pythag_trip($a,$b,$c)) {
return $a . ', ' . $b . ' and ' . $c . ' make ' . $a*$b*$c . '.';
}
}
}
}
function pythag_trip($a,$b,$c) {
if ($a**2 + $b**2 == $c**2 || $a**2 + $c**2 == $b**2 || $b**2 + $c**2 == $a**2) {
return true;
}
}
echo euler_9(1000); // Returns "200, 375 and 425 make 31875000."
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment