Fidgety Meditation
Fidgety Meditation

Reputation: 11

Error in my Java Fibonacci sequence?

public class Arrays {
public static void main(String[] args){
long Fib[] = new long[100];
Fib[0] = 1;
Fib[1] = 1;
int i = 0;
    while(i <= 100){
        Fib[i+2]= Fib[i] + Fib[i+1];
        System.out.println(Fib[i]);
        i++;
    }

}
}

I used this to find Fibonacci numbers, but it starts giving me strange readings at around the 94th term. Anyone care to explain? I'm totally new to Java, so please don't hate if it's something obvious. Here's some snippets of the error'ed output, but everything else looks fine:

832040

Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 100

1346269

...

63245986

at Arrays.main(102334155
Arrays.java:8)

165580141

...

4660046610375530309

7540113804746346429

-6246583658587674878

1293530146158671551

-4953053512429003327

-3659523366270331776

-8612576878699335103

6174643828739884737

Upvotes: 1

Views: 1977

Answers (5)

Dmitrii Bychenko
Dmitrii Bychenko

Reputation: 186668

There's no need in arrays to generate Fibonacci sequence. Another trick is to use double (long is too small for 100th Fibonacci number)

  double penultimate = 1; // <- long is not long enough ;) use double or BigInteger
  double ultimate = 1;

  System.out.println(penultimate);

  for (int i = 1; i < 100; ++i) {
    System.out.println(ultimate);

    double h = penultimate + ultimate;
    penultimate = ultimate;
    ultimate = h;
  }

Upvotes: 1

erencan
erencan

Reputation: 3763

Here is the solution. you are trying to access 102th element i + 2 where i = 100

 Fib[0] = 1;
 Fib[1] = 1;
 int i = 2;
 while(i < 100){
      Fib[i]= Fib[i-1] + Fib[i-2];
      System.out.println(Fib[i]);
      i++;
 }

Furthermore, 97th Fibonacci number exceeds long range where it is between -9,223,372,036,854,775,808 and 9,223,372,036,854,775,807. 97th Fibonacci is 83,621,143,489,848,410,000 you should use BigInteger instead of long

Below code prints until 1000 digit fibonacci number.

   BigInteger first = new BigInteger("0");
    BigInteger second = new BigInteger("1");
    BigInteger temp;// = new BigInteger("0");
    int counter = 1;

     while(numberOfDigits(second) < 1000)
     {
         temp = new BigInteger(second.toString());
         second = second.add(first);
         first = new BigInteger(temp.toString());
         counter++;
     }
     System.out.print(counter);


}

public static int numberOfDigits(BigInteger number)
{
      return number.toString().length();
}

Upvotes: 7

andersschuller
andersschuller

Reputation: 13907

When i reaches 98, Fib[i+2] will evaluate to Fib[100], which will throw an ArrayIndexOutOfBoundsException because Fib has length 100 and arrays are zero-index (as you demonstrated by assigning Fib[0]).

Also, you are getting negative results because the results are too large to fit into a long, so they are overflowing. The maximum value of a long is 9,223,372,036,854,775,807, and the 93rd Fibonacci number is the first one to exceed this with a value of 12,200,160,415,121,876,738.

Upvotes: 1

PVR
PVR

Reputation: 2524

Also, you can iterate loop 98 times to get the series. It will give you last element of Fib[100] = 6174643828739884737 .

long Fib[] = new long[100];
Fib[0] = 1;
Fib[1] = 1;
int i = 0;
    while(i < 98){
                    Fib[i+2]= Fib[i] + Fib[i+1];
                    System.out.println(Fib[i]);
                    i++;
                }

Upvotes: 0

David Pitre
David Pitre

Reputation: 181

java.lang.ArrayIndexOutOfBoundsException: 100

means that the array index 100 does not exist.

`long Fib[] = new long[100];`

creates indexes 0 - 99

Upvotes: 2

Related Questions