Question

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

Was it helpful?

Solution

(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.

OTHER TIPS

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...

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top