I made this code .. And I need to get my best. I really need a better fibonacci number calculation performance .. please help ...
I read some code for this type of computation, and I think I got the best of them.
Rate it for me .. plz ..
ps: And I really need BigInteger. I calculated Fibonacci huge numbers
ps2: I figured out some big numbers with this algorithm and I got great response time ... but I need to know if it could be better
ps3: to run this code you will need to use this VM argument -Xss16384k(StackSize)
public class Fibonacci {
private static BigInteger[] fibTmp = { BigInteger.valueOf(0), BigInteger.valueOf(1) };
public static BigInteger fibonacci(long v) {
BigInteger fib = BigInteger.valueOf(0);
if (v == 1) {
fib = BigInteger.valueOf(1);
} else if (v == 0) {
fib = BigInteger.valueOf(0);
} else {
BigInteger v1 = fibonacci(v - 1);
BigInteger v2 = fibTmp[(int) (v - 2)];
fib = v1.add(v2);
}
synchronized (fibTmp) {
if (fibTmp.length - 1 < v)
fibTmp = Arrays.copyOf(fibTmp, (int) (v + 10));
fibTmp[(int) v] = fib;
}
return fib;
}
}
source
share