# problem !!!

• 01-29-2010, 10:16 PM
jperson
problem !!!
i want to solve this problem

A Pythagorean triplet is a set of three natural numbers, a b c, for which,

a2 + b2 = c2
For example, 32 + 42 = 9 + 16 = 25 = 52.

There exists exactly one Pythagorean triplet for which a + b + c = 1000.
Find the product abc.

my code

Quote:

public class Euler {

public static void main(String[] args) {
int i = 0;
int j = 1;
int k = 2;
while (Math.pow(i, 2) + Math.pow(j, 2) != Math.pow(k, 2) && i * j * k != 1000) {
i = j;
j = k;
++k;
}
System.out.println(i*j*k);
}
}

the out always 60 although && i * j * k == 1000 in while loop:eek:

i need help
• 01-29-2010, 10:59 PM
pbrockway2
The question isn't very clear. You say at the start you want pythagorean triples whose sum is 1000. But then in the code you check the product of the triples.

Since the first time the while loop condition is checked i*j*k=0*1*2=0, the loop condition is false and the loop body will never execute. Consequently what is output at the end of main() is zero not 60 as you state.
• 01-29-2010, 11:07 PM
pbrockway2
If you change the loop condition to &&i*j*k!=1000 you will get 60 output. To see why print the corresponding values of i, j and k.

In fact if you print these values inside the loop you will get a feel for the values you are checking.