Solving Special Pythagorean Triplet (Euler Problem 9 solution)

A Pythagorean triplet is a set of three natural numbers, a < b < c, for which, a2 + b2 = c2 For example, 32 + 42 = 9 + 16 = 25 = 52. There exists exactly one Pythagorean triplet for which a + b + c = 1000. Find the product abc.
  1. /* 
  2. put this code snippet within script and call find Triplet function on body onload. 
  3. */  
  4.   
  5. var sum = 1000;  
  6. var a;  
  7. var product = 0;  
  8.   
  9. function findTriplet() { //finding triplet product  
  10.     for (a = 1; a <= sum / 3; a++) {  
  11.         var b;  
  12.         for (b = a + 1; b <= sum / 2; b++) {  
  13.             var c = sum - a - b;  
  14.             if (c > 0 && (a * a + b * b == c * c)) {  
  15.                 alert(a + ' ' + b + ' ' + c);  
  16.   
  17.                 product = (a b c);  
  18.             }  
  19.         }  
  20.     }  
  21.     alert(product);  
  22. }