【问题标题】:How to output variables from nested for loops using BigIntegers如何使用 BigIntegers 从嵌套的 for 循环中输出变量
【发布时间】:2015-04-09 18:30:09
【问题描述】:

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

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

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

【问题讨论】:

  • BigInteger max = new BigInteger("Integer.MAX_VALUE");
  • 此处相同:BigInteger sum = new BigInteger("a.add(b).add(c)");。当然,也可以是BigInteger sum = a.add(b).add(c);

标签: java biginteger eulersharp


【解决方案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)

【讨论】:

  • 另一方面是a=a.pow(4); 会覆盖a 从而导致与for 循环的意图不一致。
【解决方案2】:

我想问题更多在于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");
                }  
            }
        }
    }
}

如果我们将max 限制为4,它会生成:

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^62 才能将 a 设置为 1

【讨论】:

    【解决方案3】:

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

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

    【讨论】:

    • 非常感谢!我显然是个菜鸟,想尝试用尽方法。感谢您的知识、洞察力和改进!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-12-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-12-05
    相关资源
    最近更新 更多