4

我试图找到斐波那契数列中的第一个数字以包含 N 个数字(N 在 500 和 2000 的范围内)。我尝试使用以下代码执行此操作:

BigInteger num = BigInteger.valueOf(2);
BigInteger num1 = BigInteger.ONE;
BigInteger num2 = BigInteger.ONE;
int record = 0;
BigInteger TEN = BigInteger.valueOf(10);

public BigInteger run()
{
    BigInteger temp = BigInteger.ZERO;
    while(num2.compareTo(TEN.pow(SomeN - 1)) < 0)
    {
        if(num2.compareTo(TEN.pow(record + 1)) >= 0)
        {
            System.out.println(""+record);
            record++;
        }

        temp = num1.add(num2);
        num1 = num2;
        num2 = temp;

        num = num.add(BigInteger.ONE);
    }
    System.out.println(""+num);
    System.out.println(""+num2);
    return num2;
}

问题是,当我测试 1500 位数字时,我得到的答案显然是错误的。我不知道答案应该是什么,我什至立即检查了它周围的答案,以防我的算法被 10 次方关闭(即我检查了 1499 位和 1501),但无济于事。有人看到有什么问题吗?

4

2 回答 2

1

(删除基本提示的方式)

编辑: EP 网站已备份,我在使用您的代码时得到的答案与网站接受的对我正确的答案相匹配,早在何时。

于 2009-12-15T21:02:03.350 回答
0

当然,这里根本没有理由使用大整数形式。log10 在一行代码中就足够了。那里的比内特,做到了……

于 2009-12-16T11:43:04.127 回答