简体   繁体   中英

Finding Pythogorean Triplets Sum Given a Max Length

Here recently I've been unable to determine a solution to a problem I have. My code is skipping over certain Pythagorean Triplets during the process and therefore giving the incorrect sum.

For example if I give the max Length 15 (The hypotenuse cannot be larger than this number).

It will print:

3-4-5

5-12-13

However there are three Pythagorean triplets with the given max side length being 15:

3-4-5

8-6-10 - I'm missing this one :(

5-12-13

I understand the issue at hand, but I'm uncertain on how to create an efficient solution (without creating an unnecessary loop). Perhaps someone could point me in the right direction, and maybe simplify my code if possible. I'm always willing to learn!

class toymeister{

public static void main(String args[]){
    int inputLength = 15;

    System.out.println("Given the length: " + inputLength + 
        " the sum of the pythagorean triplets in the given range is: " 
        + sumTripletsGivenLength(inputLength));
}

public static int sumTripletsGivenLength(int length){
    boolean isFinished = false;
    int m,n = 0;
    int sum=0;

    while(!isFinished){
        n++; {
            m=n+1; {

                int a,b,c;

                a = m*m - n*n;
                b = 2*m*n;
                c = m*m + n*n;

                if(c<length){
                    System.out.println(a + "-" + b + "-" + c);
                    sum = sum+a+b+c;
                } else {
                    isFinished = true;
                }
            }
        }
    }
    return sum;
}
}

Here is a working implementation of your sumTripletsGivenLength() method:

public static int sumTripletsGivenLength(int length){
    int sum = 0;

    // the two for loops below are structured such that duplicate pairs
    // of values, e.g. (3, 4) and (4, 3), do not occur
    for (int a=1; a < length; ++a) {
        for (int b=a; b < length; ++b) {
            // calculate hypotenuse 'c' for each combintation of 'a' and 'b'
            double c = Math.sqrt(Math.pow(a, 2.0) + Math.pow(b, 2.0));

            // check if 1) 'c' be a whole number, and 2) its length is within range
            if (c == Math.ceil(c) && c < length) {
                sum = a + b + (int)c;
                System.out.println(a + "-" + b + "-" + (int)c);
            }
        }
    }

    return sum;
}

public static void main(String args[]) {
    int inputLength = 15;

    System.out.println("Given the length: " + inputLength + 
        " the sum of the pythagorean triplets in the given range is: " 
        + sumTripletsGivenLength(inputLength));
}

Output:

3-4-5
5-12-13
6-8-10
Given the length: 15 the sum of the pythagorean triplets in the given range is: 24

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