Karatsuba Algorithm for Multiplying Large Integers
Robert Keller
To compute x*y where x and y are large integers:
Let n be 1/2 of the length of the longer of x, y (in binary).
Let x1 = x / 2n and x2 = x % 2n.
Let y1 = y / 2n and y2 = y % 2n.
The above operations can be done without any actual division, by cutting
up x and y as bit strings.
Let u = x1*y1, v = x2*y2, w = (x1+x2)*(y1+y2).
Three recursive multiplications are required above.
The result of our multiplication is u*2(2*n) + (w-u-v)*2n + v.
No additional multiplications are required for the latter, since these
are again bit manipulations. (Some additions and subtractions are required.)
The result is correct, since
u*2(2*n) + (w-u-v)*2n + v =
x1*y1*2(2*n) + (x1*y1 + x1*y2 + x2*y1 + x2*y2 - x1*y1 - x2*y2)*2n + x2*y2 =
x1*y1*2(2*n) + (x1*y2 + x2*y1)*2n + x2*y2
If T(n) is the time required to multiply two n-bit integers using this
algorithm, then
T(n) = 3*T(n/2) + an + c
T(1) = b
for a constant a which takes into account the additions and constants b and c.
The solution to this recurrence can be shown to be O(n1.5) rather than O(n2)
using the obvious method.