我试图用PHP解决Euler的Project 9.我很确定我的逻辑很好,但是我的双重for循环只通过第一个循环,完成第二个循环并停止.Any1知道我做错了什么?我想做第一次998次,第二次998次.(注意:我知道我可以优化代码,因为因为a,我将永远不会高于331
//
//A Pythagorean triplet is a set of three natural numbers, a < b < c, for which,
//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.
$na = array();
$a=1;
$b=1;
$c=0;
$z=998;//upper bound
for($a;$a<$z;$a++){
for($b;$b<$z;$b++){
$c=(1000-$a)-$b;
if(($a*$a)+($b*$b)==($c*$c))
echo "played: $a + $b + $c = 1000<br />";
}
}
Run Code Online (Sandbox Code Playgroud)
结果是(当我评论if out)时:播放:1 + 1 + 998 = 1000播放:1 + 997 + 2 = 1000