1. Member Join Date
Jan 2010
Posts
61
Rep Power
0

## 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

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
Last edited by jperson; 01-29-2010 at 10:04 PM.  Reply With Quote

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

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

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

## 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.  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
•