Results 1 to 3 of 3

Thread: problem !!!

  1. #1
    jperson is offline Member
    Join Date
    Jan 2010
    Posts
    61
    Rep Power
    0

    Default 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 11:04 PM.

  2. #2
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,585
    Rep Power
    12

    Default

    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.

  3. #3
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,585
    Rep Power
    12

    Default

    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.

Similar Threads

  1. simple line problem / for loop problem
    By helpisontheway in forum New To Java
    Replies: 1
    Last Post: 11-17-2009, 07:12 AM

Posting Permissions

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