### Thread: Pythagorean Triples Help - I'm New to Java

1. No Profile Picture
Registered User
Devshed Newbie (0 - 499 posts)

Join Date
Jun 2012
Posts
6
Rep Power
0

#### Pythagorean Triples Help - I'm New to Java

Hello everyone!

Last week I began studying and practicing Java. I am trying my best to learn and as with any new study, I am running into problems that are causing some great frustration.

I have a quick question for anyone who is willing to help. It is likely a 'silly' one, but to a beginner, it is not.

I am dealing with Pythagorean triples. I am trying to create a program using loops that can display 20 possible combinations of values that will fulfill side measurements of right triangles. I am trying to write the program in a way so that congruent triangles are not represented twice.

Example of what I am not trying to do:
3,4,5
4,3,5
4,12,13
12, 4, 13
etc.

Example of what I am trying to do:
3,4,5
4,12,13,
etc.

As for the loops, my instructor is requiring me to set side1, side2, and hypot equal to 0 initially. So I try to get out of the zero zone, as seen in the first and third for commands, by adding one to side1 and one to hypot.

Now in an effort to avoid repeated values, I want to make side2 greater than side1 at all times.

Problem is, when I go to run the code, I get values that are not even Pythagorean triples.

Can anybody out there offer their assistance to a struggling beginner?

I would appreciate it greatly!
tryingtolearn

Code:
```
public class JPythagTriples
{
public static void main(String[] Theory)
{

System.out.println("Side 1\tSide 2\tHypotenuse\n");

for(side1 = 0; ((side1 + 1) * (side1 + 1)) < 2600; side1++)
{
for(side2 = (side1 + 1); ((side2 + 1) * (side2 + 1)) < 2600; side2++)
// side 2 must be greater than side 1, so values are not repeated.
{
for(hypot = 0; ((hypot + 1) * (hypot + 1)) < 2600; hypot++)
{
sideSum = ((side1 + 1) * (side1 + 1)) + ((side2 + 1) * (side2 + 1));
if(sideSum == (hypot + 1) * (hypot + 1))
{
System.out.println(side1 + "\t" + side2 + "\t" + hypot);
}
}
}
}
}
}```
2. Originally Posted by tryingtolearn
As for the loops, my instructor is requiring me to set side1, side2, and hypot equal to 0 initially.
Well that's stupid. You can't have a side of length zero. The initial value is nonsensical.
Are you sure you're understanding him/her correctly? Maybe you're supposed to initialize the variables to zero before the loops? (Which isn't great either, really.) Present the "but 0 is a bad length for a side of a triangle" argument and see if you can't get that requirement lifted.

Normally you would do something like
Code:
```for (int side1 = 1; side1*side1 < some upper limit; ++side1) {
for (int side2 = side1 + 1; side2*side2* < some upper limit; ++side2) {```
Originally Posted by tryingtolearn
Problem is, when I go to run the code, I get values that are not even Pythagorean triples.
What if you +1 the numbers that are outputted? Are those triples? Now take a look at your code and see why it's printing the wrong values.

Don't forget that you only need to print the first 20: once you've found that many you can stop.

Side comment:
You don't actually need a loop for the hypotenuse. Calculate it: sqrt(side1*side1 + side2*side2). If that value is acceptable (whole number between 1 and the upper limit) then you've found a triple. But thanks to the implications of the "set hypot=0" requirement you can't even do that. Seriously, talk to your professor about that. It's weird.
3. No Profile Picture
Registered User
Devshed Newbie (0 - 499 posts)

Join Date
Jun 2012
Posts
6
Rep Power
0
Originally Posted by requinix
Well that's stupid. You can't have a side of length zero. The initial value is nonsensical.
Are you sure you're understanding him/her correctly? Maybe you're supposed to initialize the variables to zero before the loops? (Which isn't great either, really.) Present the "but 0 is a bad length for a side of a triangle" argument and see if you can't get that requirement lifted.

Normally you would do something like
Code:
```for (int side1 = 1; side1*side1 < some upper limit; ++side1) {
for (int side2 = side1 + 1; side2*side2* < some upper limit; ++side2) {```

What if you +1 the numbers that are outputted? Are those triples? Now take a look at your code and see why it's printing the wrong values.

Don't forget that you only need to print the first 20: once you've found that many you can stop.

Side comment:
You don't actually need a loop for the hypotenuse. Calculate it: sqrt(side1*side1 + side2*side2). If that value is acceptable (whole number between 1 and the upper limit) then you've found a triple. But thanks to the implications of the "set hypot=0" requirement you can't even do that. Seriously, talk to your professor about that. It's weird.
Thank you for the help. I think youre right in that he actually said to initialize the variable values at zero.

I am still a little confused though.
4. Originally Posted by tryingtolearn
I am still a little confused though.