如果您查看此处的伪代码,您可以对其稍作修改以与数组一起使用,如下所示:
procedure karatsuba(num1, num2)
if (num1.Length < 2) or (num2.Length < 2) //Length < 2 means number < 10
return num1 * num2 //Might require another mult routine that multiplies the arrays by single digits
/* calculates the size of the numbers */
m = max(ceiling(num1.Length / 2), ceiling(num2.Length / 2))
low1, low2 = lower half of num1, num2
high1, high2 = higher half of num1, num2
/* 3 calls made to numbers approximately half the size */
z0 = karatsuba(low1,low2)
z1 = karatsuba((low1+high1),(low2+high2))
z2 = karatsuba(high1,high2)
//Note: In general x * 10 ^ y in this case is simply a left-shift
// of the digits in the 'x' array y-places. i.e. 4 * 10 ^ 3
// results in the array x[4] = { 4, 0, 0, 0 }
return (z2.shiftLeft(m*2)) + ((z1-z2-z0).shiftLeft(m)) + (z0)
如果您为数字数组定义了加法、减法和额外的一位数乘法例程,则该算法应该很容易实现(当然还有其他必需的例程,例如数字移位和数组拆分)。
因此,对于这些其他例程还有其他初步工作,但这就是 Karatsuba 例程的实施方式。