Results 1 to 8 of 8
Thread: Error with my Pythagoras triple
 12022010, 09:50 PM #1Member
 Join Date
 Dec 2010
 Posts
 6
 Rep Power
 0
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)
{
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 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+"");
}
}
}
}
}
}
}
 12022010, 09:59 PM #2Member
 Join Date
 Sep 2010
 Location
 Oregon, usa
 Posts
 69
 Rep Power
 0
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:cool: It's all here: http://download.oracle.com/javase/6/docs/api/
 12022010, 10:16 PM #3Member
 Join Date
 Dec 2010
 Posts
 6
 Rep Power
 0
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)
{
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=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");
}
}
}
}
}
 12022010, 11:21 PM #4Member
 Join Date
 Sep 2010
 Location
 Oregon, usa
 Posts
 69
 Rep Power
 0
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 hardcoded 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:cool: It's all here: http://download.oracle.com/javase/6/docs/api/
 12022010, 11:29 PM #5Member
 Join Date
 Dec 2010
 Posts
 19
 Rep Power
 0
try with while (z<=k){
but i think u will need to modify k somewhere
 12032010, 02:34 AM #6Member
 Join Date
 Dec 2010
 Posts
 6
 Rep Power
 0
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)
{
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");
}
}
}
 12032010, 03:17 AM #7Moderator
 Join Date
 Feb 2009
 Location
 New Zealand
 Posts
 4,716
 Rep Power
 17
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 ;)
Java 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"); } } }
 12032010, 03:35 AM #8Moderator
 Join Date
 Feb 2009
 Location
 New Zealand
 Posts
 4,716
 Rep Power
 17
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.
Similar Threads

java out of memory errorheap space error
By elsanthosh in forum NetBeansReplies: 4Last Post: 06152010, 10:31 AM 
> Operator cannot be applied error and return incompatible types error
By corney_16 in forum New To JavaReplies: 1Last Post: 03102010, 02:53 PM 
Thread: Error 500Internal Server Error java.lang.NullPointerException
By jackdear44 in forum New To JavaReplies: 1Last Post: 12052009, 08:28 AM 
java.lang.Error: Error opening DSound for capture
By NARs in forum NetworkingReplies: 1Last Post: 10262009, 05:38 PM 
Diference Between compiler error Garbage collection and Runtime Error?
By makpandian in forum New To JavaReplies: 3Last Post: 01232009, 09:53 AM
Bookmarks