Project Euler - Problem 9

Question

A Pythagorean triplet is a set of three natural numbers, a a² + b² = c²

For example, 3² + 4² = 9 + 16 = 25 = 5².

There exists exactly one Pythagorean triplet for which a + b + c = 1000.
Find the product abc.

Solution

$product = 0;
for ($i = 2;; $i++) {
  for ($j = 1; $j < $i; $j++) {
    $a = ($i * $i) - ($j * $j);
    $b = 2 * $i * $j;
    $c = ($i * $i) + ($j * $j);
    if ($a + $b + $c == 1000) {
      $product = $a * $b * $c;
      break 2;
    }
  }
}
echo $product;

Time taken to execute: 0.0003 second.

Answer

31875000

Add new comment