-1

当我尝试实现我为解决莱昂哈德欧拉猜想而编写的这个程序时,我遇到了疯狂的错误。错误似乎在println. 你知道我做错了什么吗?(在我运行程序之前没有错误,之后出现错误消息)我正在实现的内容相当简单,所以我不太确定它为什么不合作。

ps 我在另一个网站上阅读到将输出消息分配为字符串对象并打印该字符串对象,但这只是将另一条错误消息添加到列表中。

    public static void main(String[] args) {

    BigInteger max = new BigInteger("Integer.MAX_VALUE");

    // for(int a=0; a<max; a++)
    for(BigInteger a=BigInteger.ZERO; a.compareTo(max)<=0; a=a.add(BigInteger.ONE)){

        for(BigInteger b=BigInteger.ZERO; b.compareTo(max)<=0; b=b.add(BigInteger.ONE)){

            for(BigInteger c=BigInteger.ZERO; c.compareTo(max)<=0; c=c.add(BigInteger.ONE)){

                for(BigInteger d=BigInteger.ZERO; d.compareTo(max)<=0; d=d.add(BigInteger.ONE)){

                    // a^4
                    a=a.pow(4);
                    // b^4
                    b=b.pow(4);
                    // c^4
                    c=c.pow(4);
                    // d^4
                    d=d.pow(4);

                    // a+b+c
                    BigInteger sum = new BigInteger("a.add(b).add(c)");

                    // if( sum == d^4 )
                    int euler = sum.compareTo(d);
                    if( euler ==0)
                    {
                        System.out.println(a+"^4+"+b+"^4+"+c+"^4="+d+"^4");
                    }  
                }
            }
        }
    }   
}
4

3 回答 3

1

我想问题更多在于sum

BigInteger sum = new BigInteger("a.add(b).add(c)");

Java 通常不是一种解释型语言:它忘记了变量的名称。

另外,如果您这样做:

d=d.pow(4);

d将永远保持它的价值(不要在循环后取旧的)。

您还可以使用:

new BigInteger("Integer.MAX_VALUE");

再次无法解决,请尝试:

BigInteger.valueOf(Integer.MAX_VALUE);

错误可能通过以下方式解决:

BigInteger max = BigInteger.valueOf(Integer.MAX_VALUE);

// for(int a=0; a<max; a++)
for(BigInteger a=BigInteger.ZERO; a.compareTo(max)<=0; a=a.add(BigInteger.ONE)){

    for(BigInteger b=BigInteger.ZERO; b.compareTo(max)<=0; b=b.add(BigInteger.ONE)){

        for(BigInteger c=BigInteger.ZERO; c.compareTo(max)<=0; c=c.add(BigInteger.ONE)){

            for(BigInteger d=BigInteger.ZERO; d.compareTo(max)<=0; d=d.add(BigInteger.ONE)){

                // a^4
                BigInteger a4=a.pow(4);
                // b^4
                BigInteger b4=b.pow(4);
                // c^4
                BigInteger c4=c.pow(4);
                // d^4
                BigInteger d4=d.pow(4);

                // a+b+c
                BigInteger sum = a4.add(b4).add(c4);

                // if( sum == d^4 )
                int euler = sum.compareTo(d4);
                if( euler ==0)
                {
                    System.out.println(a+"^4+"+b+"^4+"+c+"^4="+d+"^4");
                }  
            }
        }
    }
}

如果我们限制max4,它会生成:

0^4+0^4+0^4=0^4
0^4+0^4+1^4=1^4
0^4+0^4+2^4=2^4
0^4+0^4+3^4=3^4
0^4+0^4+4^4=4^4
0^4+1^4+0^4=1^4
0^4+2^4+0^4=2^4
0^4+3^4+0^4=3^4
0^4+4^4+0^4=4^4
1^4+0^4+0^4=1^4
2^4+0^4+0^4=2^4
3^4+0^4+0^4=3^4
4^4+0^4+0^4=4^4

此外,您设计的算法效率不高。更好的方法是设置一个上界 ( max),将变量迭代到该上界,然后增加边界。否则,将需要大约2^62a设置为之前1

于 2015-04-09T18:33:59.703 回答
1

我可以立即发现两件事“可能是错误的”。

BigInteger max = new BigInteger("Integer.MAX_VALUE");

你可能想要的是:

BigInteger max = BigInteger.valueOf((long)Integer.MAX_VALUE);

并且:

BigInteger sum = new BigInteger("a.add(b).add(c)");

请尝试:

BigInteger sum = a.add(b).add(c)
于 2015-04-09T18:36:39.707 回答
1

@CommuSoft 确定了您的错误(所以接受他的回答),但我想指出您对BigIntegers 有点疯狂,并且您正在执行大量冗余计算。多得离谱。这会更有效率:

public static void main(String[] args) {
    for (int a = -1; a++ < Integer.MAX_VALUE; ) {
        BigInteger a4 = BigInteger.valueOf(a).pow(4);

        for (int b = a - 1; b++ < Integer.MAX_VALUE; ) {
            BigInteger b4 = BigInteger.valueOf(b).pow(4);
            BigInteger partialSum = a4.add(b4);

            for(int c = b - 1; c++ < Integer.MAX_VALUE; ) {
                BigInteger c4 = BigInteger.valueOf(c).pow(4);
                BigInteger sum = partialSum.add(c4);

                for(int d = c - 1; d++ < Integer.MAX_VALUE; ) {
                    BigInteger d4 = BigInteger.valueOf(d).pow(4);

                    int euler = sum.compareTo(d4);
                    if( euler == 0)
                    {
                        System.out.println(a4+"^4+"+b4+"^4+"+c4+"^4="+d4+"^4");
                    } else if (euler < 0) {
                        // d4 is larger than the sum, and will only get bigger
                        break;
                    }
                }
            }
        }
    }   
}

但是,即使进行了这些更改,我也没有看到此代码在您的有生之年运行完成。事实上,它可能不会在宇宙的生命周期内完成。

于 2015-04-09T18:55:15.653 回答