Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 10 of 10

Thread: Pythagorean Triples Help- I'm a Beginner :/

  1. #1
    Junior Member
    Join Date
    Jun 2012
    Posts
    10
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Pythagorean Triples Help- I'm a Beginner :/

    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



    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. #2
    Super Moderator pbrockway2's Avatar
    Join Date
    Jan 2012
    Posts
    987
    Thanks
    6
    Thanked 206 Times in 182 Posts

    Default Re: Pythagorean Triples Help- I'm a Beginner :/

    Problem is, when I go to run the code, I get values that are not even Pythagorean triples.
    Within the inner most loop you use the condition:

    sideSum = ((side1 + 1) * (side1 + 1)) + ((side2 + 1) * (side2 + 1));
    if(sideSum == (hypot + 1) * (hypot + 1))
    {
        // report side1/side2/hypot as a solution

    But, surely, that condition checks to see if side1+1/side2+1/hypot+1 is a pythagorean triple. Not side1/side2/hypot which is what you print. You don't say what the bogus results you get are, but it might be worth checking to see if they are "1 off" from what you expect.

    -----

    With all respect to your teacher, setting the triangle sides to zero makes little sense as self-respecting triangles have strictly positive side lengths and would shun the linear types as being "degenerate". It only seems to complicate the logic (as you've found).

  3. #3
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Pythagorean Triples Help- I'm a Beginner :/

    If you don't understand my answer, don't ignore it, ask a question.

  4. #4
    Junior Member
    Join Date
    Jun 2012
    Posts
    10
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Pythagorean Triples Help- I'm a Beginner :/

    Quote Originally Posted by pbrockway2 View Post
    Within the inner most loop you use the condition:

    sideSum = ((side1 + 1) * (side1 + 1)) + ((side2 + 1) * (side2 + 1));
    if(sideSum == (hypot + 1) * (hypot + 1))
    {
        // report side1/side2/hypot as a solution

    But, surely, that condition checks to see if side1+1/side2+1/hypot+1 is a pythagorean triple. Not side1/side2/hypot which is what you print. You don't say what the bogus results you get are, but it might be worth checking to see if they are "1 off" from what you expect.

    -----

    With all respect to your teacher, setting the triangle sides to zero makes little sense as self-respecting triangles have strictly positive side lengths and would shun the linear types as being "degenerate". It only seems to complicate the logic (as you've found).
    Thank you for your input. I have gone back and redone my code, simplifying it in the process. It works now, thanks to your advice. Here is my new code:

    public class JPythagTriples
    {
        public static void main(String[] args)
        {
             int sideA, sideB, hypot;
     
             System.out.println("\n\n\t\t\t  The Pythagorean triples are :\n");
     
             for (sideA = 0; sideA < 50; sideA++)
             {
                 for ( sideB = sideA + 1; sideB < 50; sideB++)
                 {
                     for ( hypot = 0; hypot < 50; hypot++)
                     {
                         if ( (sideA+1)*(sideA+1) + (sideB+1)*(sideB+1) == (hypot+1)*(hypot+1) )
                         {
                             System.out.println("\t\t\t\t" + (sideA + 1) + "\t" + (sideB + 1) + "\t" + (hypot + 1));
                         }
                     }
                 }
             }
        }
    }

    Now I only have one more question for you guys. I have tried looking all over the web for the answer but I have yet to find a clear, straightforward answer.

    How do I right justify the Pythagorean values in the three columns that I have created?

    Right now when I run the code, it looks like this:

    Pythagorean Values
    3 4_ 5_
    5 12 13
    6 8_ 10
    etc.
    I have inserted the underscores above to show where there is a space.

    I am trying to format it in a way that has it like this (right justified):

    3 _4 _5
    5 12 13
    etc.

    I read online something aboutd doing something with a ("%d") then a number or something, but it just isnt making sense.

    Thank you for all the help guys! I appreciate it!

    EDIT: site I went to originally for clarification on formatting: http://docs.oracle.com/javase/tutori...berformat.html
    Last edited by tryingtolearn; June 30th, 2012 at 07:25 PM.

  5. #5
    Super Moderator pbrockway2's Avatar
    Join Date
    Jan 2012
    Posts
    987
    Thanks
    6
    Thanked 206 Times in 182 Posts

    Default Re: Pythagorean Triples Help- I'm a Beginner :/

    I'm glad you've got it sorted out. Don't forget to let them know at DevShed.

    The "%d" business is used with the String format() method and with the printf() of PrintStream and elsewhere. It is documented as part of the Formatter class.

    The first thing to do with that documentation is not to panic! You aren't expected to commit it to memory or even understand it in complete detail. Rather you remember where it is and consult it as needed: learning some new "trick" each time.

    Maybe the following will be suggestive.

     
    public class FormatExamples {
        public static void main(String[] args) {
            String[] strings = {"the", "quick", "brown", "fox"};
            int[] integers = {0, -1, 666, 42};
     
            // (1)
            for(int ndx = 0; ndx < strings.length; ndx++) {
                System.out.printf("%s: %d%n", strings[ndx], integers[ndx]);
            }
            // (2)
            for(int ndx = 0; ndx < strings.length; ndx++) {
                System.out.printf("%5s%8d%n", strings[ndx], integers[ndx]);
            }
            // (3)
            for(int ndx = 0; ndx < strings.length; ndx++) {
                System.out.printf("%-5s%8d%n", strings[ndx], integers[ndx]);
            }
            // (4)
            for(int ndx = 0; ndx < strings.length; ndx++) {
                System.out.printf("%-5s%8d%10.3f%n", strings[ndx], integers[ndx], integers[ndx] * Math.PI);
            }
        }
    }

    (1) %s means "interpret the argument as a string" and %d means "interpret the argument as a decimal integer". %n means "newline" (argument not necessary) and will do the right thing on Windows and *nix (traditional and iHaveToBeDifferent). Other stuff - the colon and the space for example - is printed as is.

    (2) A number after the % means "width". Note that this makes use of tabs redundant. Setting the width is actually more reliable than using a \t character which can do different things in different circumstances.

    (3) A negative sign before the number means "left align": things are right aligned by default.

    (4) Finally a number like 10.3 means "10 wide including three digits after the decimal point". It is used for f==floating point arguments. The 3 is called "precision" and rounding is done for you.

    -----

    Much the same syntax is used in other languages (C, PHP, etc) so it's worth getting used to.
    Last edited by pbrockway2; June 30th, 2012 at 09:09 PM.

  6. #6
    Junior Member
    Join Date
    Jun 2012
    Posts
    10
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Pythagorean Triples Help- I'm a Beginner :/

    Quote Originally Posted by pbrockway2 View Post
    I'm glad you've got it sorted out. Don't forget to let them know at DevShed.

    The "%d" business is used with the String format() method and with the printf() of PrintStream and elsewhere. It is documented as part of the Formatter class.

    The first thing to do with that documentation is not to panic! You aren't expected to commit it to memory or even understand it in complete detail. Rather you remember where it is and consult it as needed: learning some new "trick" each time.

    Maybe the following will be suggestive.

     
    public class FormatExamples {
        public static void main(String[] args) {
            String[] strings = {"the", "quick", "brown", "fox"};
            int[] integers = {0, -1, 666, 42};
     
            // (1)
            for(int ndx = 0; ndx < strings.length; ndx++) {
                System.out.printf("%s: %d%n", strings[ndx], integers[ndx]);
            }
            // (2)
            for(int ndx = 0; ndx < strings.length; ndx++) {
                System.out.printf("%5s%8d%n", strings[ndx], integers[ndx]);
            }
            // (3)
            for(int ndx = 0; ndx < strings.length; ndx++) {
                System.out.printf("%-5s%8d%n", strings[ndx], integers[ndx]);
            }
            // (4)
            for(int ndx = 0; ndx < strings.length; ndx++) {
                System.out.printf("%-5s%8d%10.3f%n", strings[ndx], integers[ndx], integers[ndx] * Math.PI);
            }
        }
    }

    (1) %s means "interpret the argument as a string" and %d means "interpret the argument as a decimal integer". %n means "newline" (argument not necessary) and will do the right thing on Windows and *nix (traditional and iHaveToBeDifferent). Other stuff - the colon and the space for example - is printed as is.

    (2) A number after the % means "width". Note that this makes use of tabs redundant. Setting the width is actually more reliable than using a \t character which can do different things in different circumstances.

    (3) A negative sign before the number means "left align": things are right aligned by default.

    (4) Finally a number like 10.3 means "10 wide including three digits after the decimal point". It is used for f==floating point arguments. The 3 is called "precision" and rounding is done for you.

    -----

    Much the same syntax is used in other languages (C, PHP, etc) so it's worth getting used to.
    Thank you for the help, but I am still a little confused as to how I would properly write what I need to in order for the three columns of numbers to be right justified. Like, what would the combination of characters be, and where would I put that combination in the code in order to yield the correct format?

    Sorry if this seems like a no-brainer question, but I am trying!

    Thanks again for all your help.

  7. #7
    Super Moderator pbrockway2's Avatar
    Join Date
    Jan 2012
    Posts
    987
    Thanks
    6
    Thanked 206 Times in 182 Posts

    Default Re: Pythagorean Triples Help- I'm a Beginner :/

    The second of the four examples was designed to illustrate precisely this. It generates two right aligned columns. And it shouldn't be too difficult to come up with something that attempts to display three such columns. If that code doesn't actually compile or produce the output you intend, post it and describe the problem.

    Note there was nothing special about using the for loop: that was just to produce multiple lines of output all based on the same format. It could have been written:

    // (2)
    //for(int ndx = 0; ndx < strings.length; ndx++) {
    //    System.out.printf("%5s%8d%n", strings[ndx], integers[ndx]);
    //}
    System.out.printf("%5s%8d%n", "the", 0);
    System.out.printf("%5s%8d%n", "quick", "");
    System.out.printf("%5s%8d%n", "brown", 666);
    System.out.printf("%5s%8d%n", "fox", 42);

  8. #8
    Junior Member
    Join Date
    Jun 2012
    Posts
    10
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Pythagorean Triples Help- I'm a Beginner :/

    Quote Originally Posted by pbrockway2 View Post
    The second of the four examples was designed to illustrate precisely this. It generates two right aligned columns. And it shouldn't be too difficult to come up with something that attempts to display three such columns. If that code doesn't actually compile or produce the output you intend, post it and describe the problem.

    Note there was nothing special about using the for loop: that was just to produce multiple lines of output all based on the same format. It could have been written:

    // (2)
    //for(int ndx = 0; ndx < strings.length; ndx++) {
    //    System.out.printf("%5s%8d%n", strings[ndx], integers[ndx]);
    //}
    System.out.printf("%5s%8d%n", "the", 0);
    System.out.printf("%5s%8d%n", "quick", "");
    System.out.printf("%5s%8d%n", "brown", 666);
    System.out.printf("%5s%8d%n", "fox", 42);
    Alright, so in this example, what exactly do the '5', and '8' stand for? What are they telling the computer to do? What function do they serve?

  9. #9
    Junior Member
    Join Date
    Jun 2012
    Posts
    10
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Pythagorean Triples Help- I'm a Beginner :/

    AH! I got it! It is working perfectly now!

    Thank you so much for your patience!

  10. #10
    Super Moderator pbrockway2's Avatar
    Join Date
    Jan 2012
    Posts
    987
    Thanks
    6
    Thanked 206 Times in 182 Posts

    Default Re: Pythagorean Triples Help- I'm a Beginner :/

    You're welcome. (I guess you read point (2) in #5!) The format strings are powerful but a bit cryptic, so you master them a bit at a time.

Similar Threads

  1. Beginner
    By mkarthik90 in forum Member Introductions
    Replies: 1
    Last Post: February 18th, 2012, 02:26 PM
  2. I am a beginner please help :)
    By mvonb17 in forum What's Wrong With My Code?
    Replies: 7
    Last Post: February 6th, 2012, 03:44 PM
  3. Beginner
    By codejava in forum Member Introductions
    Replies: 2
    Last Post: August 22nd, 2011, 08:11 AM
  4. Beginner
    By angelo24 in forum Member Introductions
    Replies: 1
    Last Post: August 19th, 2011, 07:14 AM
  5. I need a help ! i am beginner
    By yinky in forum Java Theory & Questions
    Replies: 3
    Last Post: September 30th, 2009, 07:22 AM