# pithagorean triplets

• 02-07-2013, 04:11 PM
jazzo
pithagorean triplets
Hi chaps I was trying to build a program that calculates the pithagorean triplets, and I ended up with something like this:
Code:

```//TripletsTest.java public class TripletsTest{     public static void main( String[] args){         Triplets theTriplets = new Triplets(500);         theTriplets.calculateTriplets();         }      }```
and
Code:

```/* 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              }              }```
Problem is that I ended up with an infinite look and I couldn't understand why for the life of me.
Then I modified the loop sligthly to this:
Code:

```for(int i = 0; i <= limitValue && side1 <= limitValue; i++,side1++){                 side1 = Math.pow(side1, 2);                    for(int j = 0; j <= limitValue && side2 <= limitValue; j++,side2++){                     side2 = Math.pow(side2, 2);                                for(int k = 0; k <= limitValue && hypothenuse <= limitValue; k++,hypothenuse++ ){                         hypothenuse = Math.pow(hypothenuse, 2); System.out.println("\nSide1 is " + side1 + " side2 is " + side2+  " hypothenuse is " + hypothenuse);                                            if((side1 + side2) == hypothenuse ){                                 System.out.printf("%f\t: %f\t: %f\t",                                 side1, side2, hypothenuse );                                }//if                                                  }//inner loop                     }//side2 loop             }//outer loop```
thinking that perhaps I had to somehow constrain the values of the side1, side2 and hypothenuse, but alas, still got bad news, the output was
Code:

```Side1 is 0.0 side2 is 0.0 hypothenuse is 0.0 0.000000        : 0.000000      : 0.000000 Side1 is 0.0 side2 is 0.0 hypothenuse is 1.0 Side1 is 0.0 side2 is 0.0 hypothenuse is 4.0 Side1 is 0.0 side2 is 0.0 hypothenuse is 25.0 Side1 is 0.0 side2 is 0.0 hypothenuse is 676.0```
I still don't know why this doesn't work, the code seems to be okish as far as I am concerned.
Then eventually, after looking online I saw a different approach - still in the for loops - I tried that and I seems to be returning the expected result (only up to 50 and not 500 though):
Code:

```... for(int i = 1; i <= limitValue; i++){                 side1 = i;                 for(int j = 1; j <= limitValue; j++){                     side2 = j;                     for(int k = 1; k <= limitValue; k++){                         hypothenuse = k;                         if((Math.pow(side1,2)) + (Math.pow(side2,2)) == (Math.pow(hypothenuse,2))){                         //System.out.println("third loop");                                 System.out.printf("%f\t: %f\t: %f\t\n",                                 side1, side2, hypothenuse );                                }//if                     }                 }             } ...```
Output being:
Code:

```3.000000        : 4.000000      : 5.000000 4.000000        : 3.000000      : 5.000000 5.000000        : 12.000000    : 13.000000 6.000000        : 8.000000      : 10.000000 7.000000        : 24.000000    : 25.000000 8.000000        : 6.000000      : 10.000000 8.000000        : 15.000000    : 17.000000 9.000000        : 12.000000    : 15.000000 9.000000        : 40.000000    : 41.000000 10.000000      : 24.000000    : 26.000000 12.000000      : 5.000000      : 13.000000 12.000000      : 9.000000      : 15.000000 12.000000      : 16.000000    : 20.000000 12.000000      : 35.000000    : 37.000000 14.000000      : 48.000000    : 50.000000 15.000000      : 8.000000      : 17.000000 15.000000      : 20.000000    : 25.000000 15.000000      : 36.000000    : 39.000000 16.000000      : 12.000000    : 20.000000 16.000000      : 30.000000    : 34.000000 18.000000      : 24.000000    : 30.000000 20.000000      : 15.000000    : 25.000000 20.000000      : 21.000000    : 29.000000 21.000000      : 20.000000    : 29.000000 21.000000      : 28.000000    : 35.000000 24.000000      : 7.000000      : 25.000000 24.000000      : 10.000000    : 26.000000 24.000000      : 18.000000    : 30.000000 24.000000      : 32.000000    : 40.000000 27.000000      : 36.000000    : 45.000000 28.000000      : 21.000000    : 35.000000 30.000000      : 16.000000    : 34.000000 30.000000      : 40.000000    : 50.000000 32.000000      : 24.000000    : 40.000000 35.000000      : 12.000000    : 37.000000 36.000000      : 15.000000    : 39.000000 36.000000      : 27.000000    : 45.000000 40.000000      : 9.000000      : 41.000000 40.000000      : 30.000000    : 50.000000 48.000000      : 14.000000    : 50.000000```
Can anybody kindly let me know what I was doing wrong please (in plain english lol!)
thanks
• 02-10-2013, 12:57 PM
Ronin
Re: pithagorean triplets
Hi jazzo,

I do have the answer but this is an opportunity for you to understand how to debug your own code. Print statements are not the only way of finding out what is going wrong with your program. Assuming you are using a proper IDE and not just Notepad, you should be able to step through your program to find out exactly what is going on.

Just to point you in the right direction, in your first snippet the variable line2 got to infinity. In the second, your print statements are never executed after the first iteration.
You don't need to step through all 50 / 500 iterations, just the first couple are sufficient to find out the cause of your problems.

Let us know if you are still stuck.

Regards.
• 02-10-2013, 03:20 PM
JosAH
Re: pithagorean triplets
Don't try to find those triples like that; there's a more efficient way: let m < n and let a=n*n-m*m, b= 2*m*n and c= n*n+m*m then the triple (a, b, c) is a pythagorean triple. You can generate all of them by looping over m and n.

kind regards,

Jos
• 02-10-2013, 04:40 PM
jazzo
Re: pithagorean triplets
I think in the second snippet the value of the hypotenuse is so high because I wasn't increasing the value of hypotenuse correctly in that I was increasing the result of the power + 1hence I ended up with 4, then 4+1 = 5 at the 2 power so 25 etc.
I am not using an IDE to be honest, just the ubuntu console for now with a view to use netbeans in the future

In the first snippet I think side2 goes to infinity because I am not setting any constraint at all

@jos thanks but the exercise specifically said to use a triple nested loops, that's why I did it that way, I appreciate there are many ways to do it : -)!
thanks guys