开发者

Fibonacci Sequence Algorithm

I am attempting to find the first number in the Fibonacci sequence to contain N digits (N being somewhere in the range of 500 and 2000). I attempt to do this with the following code:

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);
       开发者_如何学C num1 = num2;
        num2 = temp;

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

The problem is, when I test for 1500 digits, the answer I get is apparently wrong. I do not know what the answer is supposed to be, and I have even checked the answers immediately around it in case my algorithm is off by a power of 10 (i.e. I checked 1499 digits and 1501), but to no avail. Anyone see what is wrong?


(deleted way off base hint)

EDIT: EP web site is back up, and the answer I get when using your code matches what the web site accepted as correct for me, way back when.


Of course, there is no reason to use a biginteger form here at all. log10 will suffice in one line of code. binet there, done that...

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜