Results 1 to 8 of 8
  1. #1
    anfielder is offline Member
    Join Date
    Dec 2010
    Posts
    6
    Rep Power
    0

    Post 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)
    {
    System.out.print("Please enter the value: ");
    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+"");
    }

    }
    }

    }
    }
    }
    }

  2. #2
    tashimoto is offline Member
    Join Date
    Sep 2010
    Location
    Oregon, usa
    Posts
    69
    Rep Power
    0

    Default

    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

  3. #3
    anfielder is offline Member
    Join Date
    Dec 2010
    Posts
    6
    Rep Power
    0

    Default

    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)
    {
    System.out.print("Please enter the value: ");
    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");
    }
    }

    }
    }
    }

  4. #4
    tashimoto is offline Member
    Join Date
    Sep 2010
    Location
    Oregon, usa
    Posts
    69
    Rep Power
    0

    Default

    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

  5. #5
    SmilingKey is offline Member
    Join Date
    Dec 2010
    Posts
    19
    Rep Power
    0

    Default

    try with while (z<=k){

    but i think u will need to modify k somewhere

  6. #6
    anfielder is offline Member
    Join Date
    Dec 2010
    Posts
    6
    Rep Power
    0

    Default

    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)
    {
    System.out.print("Please enter the value: ");
    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");
    }

    }
    }

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

    Default

    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)
       {
          System.out.print("Please enter the value: ");
          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");
          }
    
       }
    }

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

    Default

    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.

Similar Threads

  1. java out of memory error-heap space error
    By elsanthosh in forum NetBeans
    Replies: 4
    Last Post: 06-15-2010, 10:31 AM
  2. Replies: 1
    Last Post: 03-10-2010, 02:53 PM
  3. Replies: 1
    Last Post: 12-05-2009, 08:28 AM
  4. Replies: 1
    Last Post: 10-26-2009, 05:38 PM
  5. Replies: 3
    Last Post: 01-23-2009, 09:53 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
  •