简体   繁体   中英

Pythagorean Triples Calculation for Java

So I need help calculating Pythagorean Triples, basically I want the output to look like this:

3 4 5
5 12 13
6 8 10
7 24 25

ETC.

I need help with the calculation portion and to ensure that I do not have duplicates (ie 5 12 13 and 12 5 13).

Thoughts? Can someone lead me in the right direction?

Here is my code that I have so far:

package cs520.hw1;

public class Triples {

        public static void main(String[] args) 
        {
            int x1, x2, x3; 

            for(x1 = 1; x1 < 100; x1++)
            {
                for(x2 = 1; x2 < 100; x2++)
                {
                    for(x3 = 1; x3 < 100; x3++)
                    {
                        int a= x1, b=x2, c=x3;

                        if((Math.sqrt(a) + Math.sqrt(b)) == Math.sqrt(c))
                        {
                            if(a < b)
                            {
                                System.out.println(x1 +"  "+ x2 +"   "+ x3);
                            }
                        }
                    }
                }
            }       
        }
    }

You need to change the calls to Math.sqrt(n) to Math.pow(n, 2) , where n = a, b, c.

So, the code becomes

 package cs520.hw1;

 public class Triples {

 public static void main(String[] args) 
 {
        int x1, x2, x3; 

        for(x1 = 1; x1 < 100; x1++)
        {
            for(x2 = 1; x2 < 100; x2++)
            {
                for(x3 = 1; x3 < 100; x3++)
                {
                    int a= x1, b=x2, c=x3;

                    if((Math.pow(a, 2) + Math.pow(b, 2)) == Math.pow(c, 2))
                    {
                        if(a < b)
                        {
                            System.out.println(x1 +"  "+ x2 +"   "+ x3);
                        }
                    }
                }
            }
        }       
    }
}

Example code:

public class QuickTester {

    // Change MAX to whatever value required
    private static final int MAX = 25;

    public static void main(String[] args) {

        int a, b, c;

        for(a = 1; a < MAX; a++)
        {
            for(b = a; b < MAX; b++)
            {
                for(c = b; c < MAX; c++)
                {
                    if((Math.pow(a, 2) + Math.pow(b, 2))
                            == Math.pow(c, 2))
                    {
                        System.out.printf("%d %d %d\n",
                                a, b, c);
                    }
                }
            }
        }
    }
}

Output (for MAX being 25 instead of 100):

3 4 5
5 12 13
6 8 10
8 15 17
9 12 15
12 16 20

Note:

  • To ensure that you don't get (5 12 13 and 12 5 13), simply make sure that a < b < c.
  • Use (Math.pow(a, 2) + Math.pow(b, 2)) == Math.pow(c, 2) to find the triplets
public class Triplets {
public static void main(String args[]) {
    Scanner in = new Scanner(System.in);
    int a, b, c;
    System.out.println("Enter the value of n: ");
    int n = in.nextInt();
    System.out.println("Pythagorean Triplets upto " + n + " are:\n");
    for (a = 1; a <= n; a++) {
        for (b = a; b <= n; b++) {
            for (c = 1; c <= n; c++) {
                if (a * a + b * b == c * c) {
                    System.out.print(a + ", " + b + ", " + c);
                    System.out.println();
                }
                else
                    continue;                  
            }
        }
    }
}}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM