Divide and conquer

Multiplying two n-bit numbers

The traditional method requires O(n2) bit operations.
We'll do it in O(nlg 3) = O(n1.59) bit operations.

This algorithm is due to Karatsuba [see Knuth volume 2, pages 294-295].

Let x and y be two n-bit numbers, where n = 2m.
Then z = xy = (a2n/2 + b) (c2n/2 + d) = a*c2n + (a*d+b*c)2n/2 + b*d
This approach uses 4 multiplications (of n/2-bit numbers) + adds + shifts. (These mults are shown with *.)

Consider:
u := (a-b)*(c-d)
v := a*c
w := b*d
z := v2n + (-u+v+w)2n/2 + w
This approach uses only 3 multiplications (of n/2-bit numbers) + adds + shifts. (These mults are shown with *.)

Adds and shifts of n-bit numbers take time OB(n).   Therefore:

    T(1) = k
    T(n) = 3T(n/2) + kn, for n > 1

which is solved by:   T(n) = 3knlg 3 - 2kn

Proof by induction on n = 2m:

Basis:   n = 1 is true by inspection.

Induction step:   Assume true for n, to show true for 2n.
(Note that 3 = 2lg 3.)
T(2n) = 3T(n) + k(2n) = 3[3knlg 3 - 2kn] + 2kn = 3k(2n)lg 3 - 2k(2n),
and therefore true for 2n.


Dan Hirschberg
Last modified: Oct 28, 2003