# Thread: Error with my Pythagoras triple

1. Member 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)
{
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+"");
}

}
}

}
}
}
}  Reply With Quote

2. Member 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  Reply With Quote

3. Member 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)
{
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");
}
}

}
}
}  Reply With Quote

4. Member 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 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  Reply With Quote

5. Member Join Date
Dec 2010
Posts
19
Rep Power
0

## try with while (z<=k){

but i think u will need to modify k somewhere  Reply With Quote

6. Member 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)
{
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");
}

}
}  Reply With Quote

7. Moderator   Join Date
Feb 2009
Location
New Zealand
Posts
4,716
Rep Power
18

## 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)
{
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");
}

}
}```  Reply With Quote

8. Moderator   Join Date
Feb 2009
Location
New Zealand
Posts
4,716
Rep Power
18

## 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.  Reply With Quote

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•