Pythagorean triple in PHP

Pythagorean triple is every integer a, b and c for which a2 + b2 = c2 or in other words the a, b, and c are the sides of a right triangle. To generate triples in PHP, you can use the following function:

  function CalcTriple($n){
    $res = array();
    $res['a'] = (2*$n)+1;
    $res['b'] = (2*$n)*($n+1);
    $res['c'] = (2*$n)*($n+1)+1;
    return $res;
  }

$n is an Integer and the returned array contains the triple. Unfortunately this function WON’T generate ALL triples. It just generates valid triples. Seen on Wikipedia.

Comments

comments powered by Disqus