Try a dynamic programming approach. Instead of using recursion, the loop starts with the initial case H (0) and moves from there. Example:
public static BigInteger H(BigInteger[] c, int no, BigInteger A, BigInteger B) {
if (c.length < no - 1) {
throw new IllegalArgumentException("no is too large");
}
BigInteger bi = BigInteger.ZERO;
for (int i = 1; i <= no; i++) {
bi = bi.multiply(A).add(c[i - 1]).remainder(B);
}
return bi;
}
source
share