views:

338

answers:

3

I am attempting to find the first number in the fibbonacci 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, say 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 (ie. I checked 1499 digits and 1501), but to no avail. Anyone see what is wrong?

+1  A: 

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

Peter Recore
Thank you for not giving the answer, lol. =)
Jonathan
it seems to work fine till at least the 500th number in the sequence :)
Bozho
I double checked (again), and I do get 13 for input N=2, and 144 for N=3.
Jonathan
A: 

How big is your BigInteger? Are you certain it's big enough? :)

AlexeyMK
I don't know what the maximum size for a BigInteger is (if it has one). I don't understand exactly how it works, but I believe a BigInteger is somehow an array of numbers (type long?)... I'm not sure if it would have a limit to size. Anyways, my number is around 10^1000 (which is obvious if you read the question).
Jonathan
BigInteger is big enough. In fact, using it makes a lot of EP problems almost too easy.
Peter Recore
A: 

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

woodchips