Hi there, I have to calculate the pithagorean triplets up to 500 (it's an exercise I found on the deitel and deitel book) and I got a little stuck, in that it looks like I am getting an infinite loop. The exercise says to use a triple for loop, but obviously something is wrong there, well, I got something wrong there.
ANyway, here are the files:
//TripletsTest.java
public class TripletsTest{
public static void main( String[] args){
Triplets theTriplets = new Triplets(500);
theTriplets.calculateTriplets();
}
}
and
/*
Triplets.java
Write an application that displays a table of Pythagorean triple for side1, side2 and hypothenuse.
*/
public class Triplets{
private double side1;//double because Math.pow takes 2 double arguments
private double side2;
private double hypothenuse;
private int limitValue;
//constructor
public Triplets(int limit){
limitValue = limit;
}
public void calculateTriplets(){
for(int i = 0; i <= limitValue; i++,side1++){
side1 = Math.pow(side1, 2);
for(int j = 0; j <= limitValue; j++,side2++){
side2 = Math.pow(side2, 2);
for(int k = 0; k <= limitValue; k++,hypothenuse++ ){
hypothenuse = Math.pow(hypothenuse, 2);
if((side1 + side2) == hypothenuse ){
System.out.printf("%f\t: %f\t: %f\t",
side1, side2, hypothenuse );
}//if
}//inner loop
}//side2 loop
}//outer loop
}
}
Any suggestion at all?
thanks