Karatsuba算法的实现,方法只算小数为真,但大答案不正确,有什么问题?
var x = "1685287499328328297814655639278583667919355849391453456921116729";
var y = "7114192848577754587969744626558571536728983167954552999895348492";
function karatsuba(x, y) {
if (x.length < 2 && y.length < 2) {
return BigInt(parseInt(x) * parseInt(y));
}
var m = Math.min(x.length, y.length);
var m2 = m / 2;
var a = BigInt(x.substring(0, m2));
var b = BigInt(x.substring(m2));
var c = BigInt(y.substring(0, m2));
var d = BigInt(y.substring(m2));
var ac = a * c;
var bd = b * d;
var sum = (a + b) * (c + d) - ac - bd;
return BigInt(Math.pow(10, m)) * ac + BigInt(Math.pow(10, m2)) * sum + bd;
}
console.log(karatsuba(x, y));