1. Gyanastra
  2. Algorithms
  3. Strassen, Karatsuba and Closest Pair of Points
  4. Karatsuba's Integer Multiplication
Free lesson, no account needed 4 min read

Algorithms · Strassen, Karatsuba and Closest Pair of Points

Karatsuba's Integer Multiplication

2.1 The naive method

Multiplying two nn-digit numbers the schoolbook way: each digit of one number multiplies each digit of the other, giving n2n^2 single-digit multiplications, plus O(n)O(n) additions/shifts. Time: Θ(n2)\Theta(n^2).

2.2 The divide-and-conquer split

Write each nn-digit number in two halves of n/2n/2 digits each (assume nn is a power of 2; pad with leading zeros otherwise). Let B=10n/2B=10^{n/2} (the "base"). Then

x=x1B+x0,y=y1B+y0x = x_1 B + x_0,\qquad y = y_1 B + y_0

where x1,x0,y1,y0x_1,x_0,y_1,y_0 each have n/2n/2 digits. Then

xy=x1y1 B2+(x1y0+x0y1) B+x0y0xy = x_1y_1\,B^2 + (x_1y_0+x_0y_1)\,B + x_0y_0

The naive D&C computes 4 products: x1y1x_1y_1, x1y0x_1y_0, x0y1x_0y_1, x0y0x_0y_0. Recurrence T(n)=4T(n/2)+Θ(n)T(n)=4T(n/2)+\Theta(n) gives Θ(n2)\Theta(n^2) — no gain.

2.3 Karatsuba's trick: 3 products

Observe that

x1y0+x0y1=(x1+x0)(y1+y0)−x1y1−x0y0x_1y_0+x_0y_1 = (x_1+x_0)(y_1+y_0) - x_1y_1 - x_0y_0

So compute only three products:

P1=x1y1,P2=x0y0,P3=(x1+x0)(y1+y0)P_1 = x_1y_1,\qquad P_2 = x_0y_0,\qquad P_3=(x_1+x_0)(y_1+y_0)

Then

xy=P1 B2+(P3−P1−P2) B+P2xy = P_1\,B^2 + (P_3-P_1-P_2)\,B + P_2

The middle coefficient P3−P1−P2P_3-P_1-P_2 equals x1y0+x0y1x_1y_0+x_0y_1. The additions/subtractions and shifts cost Θ(n)\Theta(n).

2.4 Recurrence and complexity

T(n)=3 T(n/2)+Θ(n)T(n)=3\,T(n/2)+\Theta(n)

Master Theorem: a=3a=3, b=2b=2, f(n)=Θ(n)f(n)=\Theta(n). log⁡ba=log⁡23≈1.585\log_b a=\log_2 3\approx 1.585. Since f(n)=O(n1.585−ε)f(n)=O(n^{1.585-\varepsilon}), Case 1:

T(n)=Θ(nlog⁡23)=Θ(n1.585…)T(n)=\Theta(n^{\log_2 3})=\Theta(n^{1.585\ldots})

Number of single-digit multiplications: 3log⁡2n=nlog⁡233^{\log_2 n}=n^{\log_2 3}.

2.5 Worked 2-digit example

Multiply x=12x=12 and y=34y=34 (both 2-digit, so n=2n=2, B=101=10B=10^{1}=10).

Split: x1=1, x0=2x_1=1,\ x_0=2; y1=3, y0=4y_1=3,\ y_0=4.

  • P1=x1y1=1×3=3P_1 = x_1y_1 = 1\times 3 = 3
  • P2=x0y0=2×4=8P_2 = x_0y_0 = 2\times 4 = 8
  • P3=(x1+x0)(y1+y0)=(1+2)(3+4)=3×7=21P_3 = (x_1+x_0)(y_1+y_0) = (1+2)(3+4) = 3\times 7 = 21

Middle coefficient: P3−P1−P2=21−3−8=10P_3-P_1-P_2 = 21-3-8 = 10.

Result: P1B2+(P3−P1−P2)B+P2=3×100+10×10+8=300+100+8=408P_1 B^2 + (P_3-P_1-P_2)B + P_2 = 3\times 100 + 10\times 10 + 8 = 300+100+8 = 408.

Check directly: 12×34=40812\times 34 = 408. ✓

Second example (with a carry in the middle term): x=56x=56, y=78y=78. x1=5,x0=6x_1=5,x_0=6; y1=7,y0=8y_1=7,y_0=8.

  • P1=5×7=35P_1=5\times7=35
  • P2=6×8=48P_2=6\times8=48
  • P3=(5+6)(7+8)=11×15=165P_3=(5+6)(7+8)=11\times15=165
  • Middle: 165−35−48=82165-35-48=82

Result: 35×100+82×10+48=3500+820+48=436835\times100 + 82\times10 + 48 = 3500+820+48=4368. Check: 56×78=436856\times78 = 4368. ✓

(Note the middle coefficient 8282 exceeds the base 10 — that is fine; the formula is an exact integer identity, carries are handled automatically.)

2.6 Comparison

MethodRecurrenceMultiplicationsTime
Schoolbook—n2n^2Θ(n2)\Theta(n^2)
Naive D&C (4 products)T(n)=4T(n/2)+Θ(n)T(n)=4T(n/2)+\Theta(n)n2n^2Θ(n2)\Theta(n^2)
Karatsuba (3 products)T(n)=3T(n/2)+Θ(n)T(n)=3T(n/2)+\Theta(n)nlog⁡23n^{\log_2 3}Θ(nlog⁡23)≈Θ(n1.585)\Theta(n^{\log_2 3})\approx\Theta(n^{1.585})

Convention note. Some texts write the additive term as Θ(n)\Theta(n) (digit additions), others as O(n)O(n); the exponent log⁡23\log_2 3 is the same. GATE uses log⁡23≈1.585\log_2 3\approx 1.585.


Pages 1–2 of 2