Doubt about Fibonacci series

Asked

Viewed 32 times

0

Why didn’t my code print the first 300 numbers in the Fibonacci series and start finding negative numbers? What’s the problem with this code? Because he only goes up to 92?

 final int ARRAY_SIZE = 300; 
    long[] fib = new long[ARRAY_SIZE];


    fib[0] = 0; fib[1] = 1; 
    int n = 2; 
    boolean tooBig = false; 


    while(!tooBig){  
      fib[n] = fib[n-1]+fib[n-2]; //formula!
      if(fib[n]<fib[n-1]) {
    tooBig=true;
  }else
    n++;
}


println("The first "+n+" Fibonacci numbers are:");
//Experiment: Try changing "i<n" to "i<=n".
for(int i=0; i<=n; i++){
  println("Fib: " + i + " " + fib[i]+ " ");
  }
  • Ok, but I find the error in my code. In the given answered questions is not being used arrays.

  • How do I change my code to use Biginterger? I imported the java class, but it’s still in error.

No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.