I am given a number k and I have to find every (2k) factorial from [0;k]; for example 0, 2!, 4!, 6!, etc. I have attempted a solution to save the values in a map and for each kth value to use the (k-1)th result like this:
private Map<Long, BigInteger> cache;
private FactorialCache(int k) {//
cache = new HashMap<>();
calculate(k);
System.out.println("last item " + k);
}
private void calculate(int k) {
BigInteger result = BigInteger.ONE;
cache.put(0l, result);
cache.put(1l, result);
for (long i = 2; i <= k; i += 1) {
BigInteger currentRes = cache.get(i - 1).multiply(BigInteger.valueOf(i));
cache.put(i, currentRes);
}
}
However, I was curious if there was a faster approach to find and save these specific factorials?