# Error with my Pythagoras triple

• 12-02-2010, 09:50 PM
anfielder
Error with my Pythagoras triple
Here's my Pythagoras triple code. I always get an error on the x*x + y*y = z*z
import java.util.Scanner;
public class Triples
{
public static void main(String [] args)
{
Scanner input = new Scanner(System.in);
int k = input.nextInt();
if (k<1)
{
System.out.println("Please enter a number greater than 1");
}
else
{
int x,y,z;
while (z <= k)
{
while (z == Math.sqrt(x*x + y*y));
{
while (int check = Math.sqrt(x*x + y*y));
{
if (check%2 == 0)
{
System.out.println(""+x+", "+y+" "+z+"");
}

}
}

}
}
}
}
• 12-02-2010, 09:59 PM
tashimoto
It seems that you haven't given x, y, or z any starting values.... in your first while loop, what is the value of z when testing whether it is less than or equal to k ??

Hope this helps!
Chris
• 12-02-2010, 10:16 PM
anfielder
So I've made some modifications and it works with twist!
It only produces the first string (3,4,5).
Any ideas why?
Here's the code:
import java.util.Scanner;
public class Triples
{
public static void main(String [] args)
{
Scanner input = new Scanner(System.in);
int k = input.nextInt();
if (k<1)
{
System.out.println("Please enter a number greater than 1");
}
else
{
int z=5, y=3, x=4;
{
while (z<=k)
if (z*z == y*y + x*x)
{
System.out.println(""+x+","+y+","+z+"");
}
else
{
System.out.println("Fail");
}
}

}
}
}
• 12-02-2010, 11:21 PM
tashimoto
What is your code supposed to do? You say that it only produces the first string; what was your input for "k", and what were you expecting as the output? Are the values of z, y, and x supposed to be hard-coded or is there supposed to be some math being done with an input "k" to calculate x, y, z??

I'm unfamiliar with Pythagorean Triple so I did a quick search online and found this:
Pythagorean triple - Wikipedia, the free encyclopedia
Maybe there is something in there that will help. :)

Kind regards,
Chris
• 12-02-2010, 11:29 PM
SmilingKey
try with while (z<=k){

but i think u will need to modify k somewhere
• 12-03-2010, 02:34 AM
anfielder
I found what my error was. The whole idea of the code is to find all the Pythagorean combinations that are lower than k (where by k is the input).
I had to change and use 'for' instead of 'while', which repaired a lot of things. Also, I changed a few definitions for 'x' & 'y'. Check out the end product and please tell me if there is a simpler version using 'while' loops as I prefer them to 'for' loops.

import java.util.Scanner;
public class Triples
{
public static void main(String [] args)
{
Scanner input = new Scanner(System.in);
int k = input.nextInt();
if (k<1)
{
System.out.println("Please enter a number greater than 1");
}
else
{
int z,y,x,n=0;
for (z=1; z<=k; z++)
{
for (y=1; y<=k; y++)
{
for (x=y; x<=k; x++)
{
if (z*z == y*y + x*x)
{
n++;
System.out.println(""+n+": "+y+","+x+","+z+"");

}

}

}

}
System.out.println("\nThere are "+n+" Pythagorean tripples less than "+k+"\n");
}

}
}
• 12-03-2010, 03:17 AM
pbrockway2
Please use the code tags when pasting code: put [CODE] at the start of your code and [/CODE] at the end. I'll copy your code now with this so I can read it ;)

Code:

```import java.util.Scanner; public class Triples {   public static void main(String [] args)   {       System.out.print("Please enter the value: ");       Scanner input = new Scanner(System.in);       int k = input.nextInt();       if (k<1)       {         System.out.println("Please enter a number greater than 1");       }       else       {         int z,y,x,n=0;         for (z=1; z<=k; z++)         {             for (y=1; y<=k; y++)             {               for (x=y; x<=k; x++)               {                   if (z*z == y*y + x*x)                   {                     n++;                     System.out.println(""+n+": "+y+","+x+","+z+"");                     }                 }             }           }         System.out.println("\nThere are "+n+" Pythagorean tripples less than "+k+"\n");       }   } }```
• 12-03-2010, 03:35 AM
pbrockway2
Personally I prefer for loops if the start/stop and increment are all known "upfront" as is the case here. Basically that's why they were invented.

I would also prefer more meaningful variable names. I had to read it a couple of times before I realised that you were correctly identifying duplicates. If z/y/x had been hyp/shorter/longer it would have been clearer.

Note that hyp<Math.sqrt(k) shorter<hyp and shorter<longer<hyp (all inequalities are strict) which might save the CPU a few cycles. Moreover once you have found a triple you can break out of the innermost loop.

----------

Not that you were asking what I prefer...

You could nest some while loops and break when the hyp^2 gets too big, I suppose. Not necessarily "simpler" though.