Project Euler Problem 9 (PHP)

Share the joy
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  

Problem 9
A Pythagorean triplet is a set of three natural numbers, a < b < c, for which, a^2 + b^2 = c^2 For example, 3^2 + 4^2 = 9 + 16 = 25 = 52. There exists exactly one Pythagorean triplet for which a + b + c = 1000. Find the product abc.

Script

Execution Time: 17.3896 seconds

$addTotal = 1000;

for ($x=1; $x<$addTotal; $x++) {
    for ($y=$x+1; $y<$addTotal; $y++) {
        for ($z=$y+1; $z<=$addTotal; $z++) {
            if (($x+$y+$z == $addTotal) && (($x*$x)+($y*$y) == ($z*$z))) {
                echo ($x*$y*$z).PHP_EOL;
                break 3;
            }
        }
    }
}

Share the joy
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
This entry was posted in PHP, Programming, Project Euler and tagged , , , . Bookmark the permalink.

Comments are closed.