Tuesday, January 8, 2013

Problem 9: Special Pythagorean triplet


Q)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.

Solution:
Java Code



public class Prob9 {

 
 public static void main(String[] args) {
 
     int sum = 1000;
      int a;
      int b;
      int c;
      for (a = 1; a <= sum/3; a++)
      {
      for (b = a + 1; b <= sum/2; b++)
          {
              c = sum - a - b;
              if ( c > 0 && (a*a + b*b == c*c) )
              {
              System.out.println(a*b*c);
              }
          }    
          
      
 }
 }
}



1 comment: