omgpop
BAN USER
Accountant at ADP
Comments (4)
Reputation 5
Page:
1
Comment hidden because of low score. Click to expand.
Comment hidden because of low score. Click to expand.
Comment hidden because of low score. Click to expand.
Comment hidden because of low score. Click to expand.
0
of 0 vote
import java.math.BigInteger;
import java.util.ArrayList;
public class FibonacciMemoized {
// Memoization cache
private static ArrayList<BigInteger> fibCache = new ArrayList<BigInteger>();
static {
fibCache.add(BigInteger.ZERO);
fibCache.add(BigInteger.ONE);
}
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<=Integer.parseInt(args[0]); i++)
System.out.print(fib(i)+", ");
}
}
Page:
1
CareerCup is the world's biggest and best source for software engineering interview preparation. See all our resources.
- omgpop November 12, 2012