我正在尝试使用 Java 起诉 BigInteger 来实现 karatsuba 算法,我遵循了所有步骤,但我没有得到正确的结果,是什么让我抓狂。
这是我的代码:
public BigInteger karatsuba(BigInteger a, BigInteger b, int base) {
if (a.compareTo(BigInteger.TEN) == -1 || b.compareTo(BigInteger.TEN) == -1 ) {
return a.multiply(b);
}
/* calculates the size of the numbers */
int tam = a.toString().length();
int mitad = tam/2;
BigInteger a1 = (a.divide(BigInteger.valueOf((long) Math.pow(base, mitad))));
BigInteger a0 = (a.remainder(BigInteger.valueOf((long) Math.pow(base, mitad))));
BigInteger b1 = (b.divide(BigInteger.valueOf((long) Math.pow(base, mitad))));
BigInteger b0 = (b.remainder(BigInteger.valueOf((long) Math.pow(base, mitad))));
/* 3 calls made to numbers approximately half the size */
BigInteger t1 = karatsuba(a1, b1, base);
BigInteger t2 = karatsuba(a0, b0, base);
BigInteger t3 = karatsuba(a1.add(a0), b1.add(b0), base);
BigInteger aux1 = (t1.multiply(BigInteger.valueOf((long)Math.pow(base, tam))));
BigInteger aux2 = t1.subtract(t2);
BigInteger aux4 = aux2.subtract(t3);
BigInteger aux3 = aux4.multiply(BigInteger.valueOf((long) Math.pow(base,mitad)).add(t2));
return aux1.add(aux3);
}
我使用以下条目测试了代码:karatsuba(new BigInteger("1252",new BigInteger("532") , 10)
虽然正确的结果是 666064 ,但我得到 2212864 !!!。我调试了,令人惊讶的是,当执行流程到达 return 语句时,程序并没有停止,而是进入了BigInteger t2 = karatsuba(a0, b0, base);
语句。
所以我不知道我做错了什么。任何帮助将不胜感激。