您好我正在尝试在 Javascript 中实现 karatsuba 算法。截至目前,该算法适用于某些情况,例如整数长度为 4 或 8 时。当整数长度为 6 时,它会打印出错误的结果
例如:3141*2718=>8537238(正确结果)
例如:314*271=>84981.37398106341(结果不正确)
Eg: 3141592653589793238462643383279502884197169399375105820974944592 * 2718281828459045235360287471352662497757247093699959574966967627 =>8.539734222673569e+126 (partially correct )
var firstnumber= 3141592653589793238462643383279502884197169399375105820974944592
var secondNumber=2718281828459045235360287471352662497757247093699959574966967627
Karatsuba(firstnumber,secondNumber)
function Karatsuba(x,y)
{
if(x<10 || y<10)
{
return x*y
}
var first=Math.ceil(Math.log(x + 1) / Math.LN10)
var second=Math.ceil(Math.log(x + 1) / Math.LN10)
var min=Math.min(first,second);
var a=Math.floor(x/Math.pow(10,min/2))
var b=Math.floor(x%Math.pow(10,min/2))
var c=Math.floor(y/Math.pow(10,min/2))
var d=Math.floor(y%Math.pow(10,min/2))
var s=Math.pow(10,min/2)
return ((Math.pow(10,min))*Karatsuba(a,c)+ s*(Karatsuba(a,d) +Karatsuba(b,c)) + Karatsuba(b,d))
}