Results 1 to 4 of 4
Thread: pithagorean triplets
 02072013, 03:11 PM #1Member
 Join Date
 Feb 2013
 Posts
 18
 Rep Power
 0
pithagorean triplets
Hi chaps I was trying to build a program that calculates the pithagorean triplets, and I ended up with something like this:
Java Code://TripletsTest.java public class TripletsTest{ public static void main( String[] args){ Triplets theTriplets = new Triplets(500); theTriplets.calculateTriplets(); } }
Java 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 } }
Then I modified the loop sligthly to this:
Java 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
Java 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
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):
Java 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 } } } ...
Java 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
thanks
 02102013, 11:57 AM #2Senior Member
 Join Date
 Oct 2010
 Posts
 317
 Rep Power
 4
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.
 02102013, 02:20 PM #3
 Join Date
 Sep 2008
 Location
 Voorschoten, the Netherlands
 Posts
 13,458
 Blog Entries
 7
 Rep Power
 20
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*nm*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,
Joscenosillicaphobia: the fear for an empty beer glass
 02102013, 03:40 PM #4Member
 Join Date
 Feb 2013
 Posts
 18
 Rep Power
 0
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
Similar Threads

Apache Lucene gives birth to triplets!
By patric78 in forum LuceneReplies: 0Last Post: 04282010, 10:51 AM
Bookmarks