fibonacci series


See On Github

Data

Tags

recursion

Source Code

Implementation of fibonacci using memoization.
// Defining fibcache to store the fibonacci numbers.
//It is an array list of biginteger. 
private static ArrayList<BigInteger> fibCache = new ArrayList<BigInteger>();
static {
      fibCache.add(BigInteger.ZERO);
      fibCache.add(BigInteger.ONE);
}
import java.math.BigInteger;
import java.util.ArrayList;
// If the found number is already in the cache it will return that 
//number else it will calculate and store that number in cache.
public class FibonacciMemoized {
        

       public static BigInteger fib(int n) {
              if (n >= fibCache.size()) {
                  fibCache.add(n, fib(n-1).add(fib(n-2)));
              }
              return fibCache.get(n);
       }

}
public static void main(String[] args) {
    for (int i=0; i<=40; i++)
        System.out.print(fib(i)+", ");
}