Java 求给定最大长度的Pythogorean三元组和

Java 求给定最大长度的Pythogorean三元组和,java,math,Java,Math,最近在这里,我无法确定一个问题的解决方案。在这个过程中,我的代码跳过了某些毕达哥拉斯三元组,因此给出了错误的和 例如,如果我给最大长度15(斜边不能大于这个数字) 它将打印: 3-4-5 5-12-13 但是,有三个毕达哥拉斯三胞胎,给定的最大边长为15: 3-4-5 8-6-10-我错过了这个:( 5-12-13 我理解眼前的问题,但我不确定如何创建一个有效的解决方案(而不创建不必要的循环)。也许有人可以为我指出正确的方向,如果可能的话,也许可以简化我的代码。我始终愿意学习 class toy

最近在这里,我无法确定一个问题的解决方案。在这个过程中,我的代码跳过了某些毕达哥拉斯三元组,因此给出了错误的和

例如,如果我给最大长度15(斜边不能大于这个数字)

它将打印:

3-4-5

5-12-13

但是,有三个毕达哥拉斯三胞胎,给定的最大边长为15:

3-4-5

8-6-10-我错过了这个:(

5-12-13

我理解眼前的问题,但我不确定如何创建一个有效的解决方案(而不创建不必要的循环)。也许有人可以为我指出正确的方向,如果可能的话,也许可以简化我的代码。我始终愿意学习

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;
}
}
类toymeister{
公共静态void main(字符串参数[]){
int输入长度=15;
System.out.println(“给定长度:+inputLength+
“给定范围内毕达哥拉斯三联体的总和为:”
+sumTripletsGivenLength(输入长度));
}
公共静态int-sumTripletsGivenLength(int-length){
布尔值isFinished=false;
int m,n=0;
整数和=0;
而(!isFinished){
n++{
m=n+1{
INTA、b、c;
a=m*m-n*n;
b=2*m*n;
c=m*m+n*n;

如果(c这里是您的
sumTripletsGivenLength()
方法的工作实现:

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));
}

你能详细说明一下你打算用来扫描可能的三元组组合的算法吗?它的难以捉摸的公式是这样的。对于任意两个n>m的正整数(m和n)。A=m^2-n^2,b=2mn,c=m^2+n^2我对Math.pow的实现有点困惑。你能详细说明一下吗?当然。
Math.pow(3.0,2.0)
表示
3^2
或3平方。我将其与毕达哥拉斯方程一起使用,该方程表示
c=SQRT(a^2+b^2)
。这就是我计算每个潜在斜边的方法。当然,它也必须是一个整数。代码
c==Math.ceil(c)
检查每个斜边
c
是否是一个整数。哦,我现在明白了。真不敢相信我第一次没看到。
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