The basic idea is this: The number 237 is the polynomial p(x)=2x2+3x+7 evaluated at the point x=10. So, we can think of each integer corresponding to a polynomial whose coefficients are the digits of the number. When we evaluate the polynomial at x=10, we get our number back.
What is interesting is that to fully specify a polynomial of degree 2, we need its value at just 3 distinct points. We need 5 values to fully specify a polynomial of degree 4.
So, if we want to multiply two 3 digit numbers, we can do so by:
- Evaluating the corresponding polynomials at 5 distinct points.
- Multiplying the 5 values. We now have 5 function values of the polynomial of the product.
- Finding the coefficients of this polynomial from the five values we computed in step 2.
Karatsuba multiplication works the same way, except that we only need 3 distinct points. Instead of at 10, we evaluate the polynomial at 0, 1, and "infinity", which gives us b,a+b,a
and d,d+c,c
which multiplied together give you your X,Z,Y
.
Now, to write this all out in terms of abc
and def
is quite involved. In the Wikipedia article, it's actually done quite nicely:
- In the Evaluation section, the polynomials are evaluated to give, for example,
c,a+b+c,a-b+c,4a+2b+c,a
for the first number.
- In Pointwise products, the corresponding values for each number are multiplied, which gives:
X = cf
Y = (a+b+c)(d+e+f)
Z = (a+b-c)(d-e+f)
U = (4a+2b+c)(4d+2e+f)
V = ad
- In the Interpolation section, these values are combined to give you the digits in the product. This involves solving a 5x5 system of linear equations, so again it's a bit more complicated than the Karatsuba case.