Sunday, October 12, 2014

Pythagorean triplet

Ques:
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.

Ans:

package gradesforeexam;


public class GradesforeExam {

     public static void main(String[] args) {
        double a=0, b=0, c = 0;
        for (int i = 1; i < 1000; i++) {
            for (int j=i+1;j<1000;j++ ) {
                c = Math.pow(Math.pow(i, 2) + Math.pow(j, 2), 0.5);
               
                if ((c - (int) c == 0.0)&&(i+j+(int)c==1000)) {
                   System.out.println(a*b*c);
                   }
            }
        }
    }

}

No comments:

Post a Comment