Algorithms · Strassen, Karatsuba and Closest Pair of Points
Karatsuba's Integer Multiplication
2.1 The naive method
Multiplying two n-digit numbers the schoolbook way: each digit of one number multiplies each digit of the other, giving n2 single-digit multiplications, plus O(n) additions/shifts. Time: Θ(n2).
2.2 The divide-and-conquer split
Write each n-digit number in two halves of n/2 digits each (assume n is a power of 2; pad with leading zeros otherwise). Let B=10n/2 (the "base"). Then
x=x1B+x0,y=y1B+y0
where x1,x0,y1,y0 each have n/2 digits. Then
xy=x1y1B2+(x1y0+x0y1)B+x0y0
The naive D&C computes 4 products: x1y1, x1y0, x0y1, x0y0. Recurrence T(n)=4T(n/2)+Θ(n) gives Θ(n2) — no gain.
2.3 Karatsuba's trick: 3 products
Observe that
x1y0+x0y1=(x1+x0)(y1+y0)−x1y1−x0y0
So compute only three products:
P1=x1y1,P2=x0y0,P3=(x1+x0)(y1+y0)
Then
xy=P1B2+(P3−P1−P2)B+P2
The middle coefficient P3−P1−P2 equals x1y0+x0y1. The additions/subtractions and shifts cost Θ(n).
2.4 Recurrence and complexity
T(n)=3T(n/2)+Θ(n)
Master Theorem: a=3, b=2, f(n)=Θ(n). logba=log23≈1.585. Since f(n)=O(n1.585−ε), Case 1:
T(n)=Θ(nlog23)=Θ(n1.585…)
Number of single-digit multiplications: 3log2n=nlog23.
2.5 Worked 2-digit example
Multiply x=12 and y=34 (both 2-digit, so n=2, B=101=10).
(Note the middle coefficient 82 exceeds the base 10 — that is fine; the formula is an exact integer identity, carries are handled automatically.)
2.6 Comparison
Method
Recurrence
Multiplications
Time
Schoolbook
—
n2
Θ(n2)
Naive D&C (4 products)
T(n)=4T(n/2)+Θ(n)
n2
Θ(n2)
Karatsuba (3 products)
T(n)=3T(n/2)+Θ(n)
nlog23
Θ(nlog23)≈Θ(n1.585)
Convention note. Some texts write the additive term as Θ(n) (digit additions), others as O(n); the exponent log23 is the same. GATE uses log23≈1.585.