Sub topic 13
Karatsuba: Multiplying with Three Subproblems Instead of Four
This chapter assumes you can read recursive Python, know Big-O notation, and can apply the master theorem to a recurrence of the form T(n) = aT(n/b) + f(n). Earlier chapters covered all three.
The algorithms so far gained speed by splitting the input in a clever way. Karatsuba multiplication shows another approach. It splits the input in the obvious way and then needs fewer recursive calls than the obvious method. Cutting four subproblems down to three changes the exponent in the running time. Strassen's algorithm in the next chapter uses the same idea on matrices.
The Problem: Multiplying Large Integers
To multiply two n-digit numbers by hand, you multiply every digit of one by every digit of the other. That takes n² single-digit products, so the schoolbook method is O(n²). For numbers with millions of digits, as in cryptography, computer algebra, and arbitrary-precision arithmetic, n² is too slow.
The Obvious Divide-and-Conquer Split
Write each number as a high half and a low half. In base B, with m digits in the low half:
- x = a · B^m + b
- y = c · B^m + d
Multiplying out gives:
xy = ac · B^(2m) + (ad + bc) · B^m + bd
That takes four half-size products: ac, ad, bc, and bd. Multiplying by B^m is a shift (appending zeros, or shifting bits in binary), so it costs O(n). The additions also cost O(n). The recurrence is:
T(n) = 4T(n/2) + O(n)
In master theorem terms, a = 4 and b = 2, so n^(log₂ 4) = n². That term dominates the O(n) combine cost, so T(n) = O(n²). Splitting the numbers alone gained nothing. The recursion just reorganizes the schoolbook method.
Karatsuba's Trick
The final answer does not need ad and bc separately. It needs only their sum. Karatsuba's observation (1960) is that one product gives you that sum:
(a + b)(c + d) = ac + ad + bc + bd
You already compute ac and bd for the outer terms. Subtract them and the middle term remains:
ad + bc = (a + b)(c + d) − ac − bd
So three multiplications are enough:
- z₂ = ac
- z₀ = bd
- z₁ = (a + b)(c + d) − z₂ − z₀
The result is z₂ · B^(2m) + z₁ · B^m + z₀.
A Worked Example
Multiply 1234 × 5678 in base 10 with m = 2.
| Quantity | Value |
|---|---|
| a, b | 12, 34 |
| c, d | 56, 78 |
| z₂ = ac | 672 |
| z₀ = bd | 2652 |
| (a + b)(c + d) = 46 × 134 | 6164 |
| z₁ = 6164 − 672 − 2652 | 2840 |
| 672 · 10⁴ + 2840 · 10² + 2652 | 7006652 |
You can check that 1234 × 5678 = 7006652. The algorithm used three two-digit multiplications where the obvious split uses four, and each of those can recurse in the same way.
The New Recurrence
With three subproblems the recurrence becomes:
T(n) = 3T(n/2) + O(n)
Now a = 3 and b = 2, so n^(log₂ 3) ≈ n^1.585. That term still dominates the O(n) combine cost, so case 1 of the master theorem gives:
T(n) = O(n^(log₂ 3)) ≈ O(n^1.585)
In the recursion tree, level i has 3^i nodes, each doing O(n/2^i) work. The work per level is n · (3/2)^i. That is a growing geometric series, so the leaves dominate. There are 3^(log₂ n) = n^(log₂ 3) leaves.
| Digits n | Leaf products with 4 subproblems (4^k) | Leaf products with Karatsuba (3^k) |
|---|---|---|
| 16 | 256 | 81 |
| 256 | 65,536 | 6,561 |
| 1,024 | 1,048,576 | 59,049 |
At 1,024 digits, Karatsuba does about 18 times fewer single-digit products. The gap keeps widening as n grows.
Implementation in Binary
Computers store integers in binary, so a real implementation splits on bits. Shifting by m bits multiplies by 2^m, and masking with (1 << m) − 1 extracts the low half.
import random
def karatsuba(x, y):
# Multiplies non-negative integers x and y
if x < 10 or y < 10:
return x * y
m = max(x.bit_length(), y.bit_length()) // 2
hi_x, lo_x = x >> m, x & ((1 << m) - 1)
hi_y, lo_y = y >> m, y & ((1 << m) - 1)
z0 = karatsuba(lo_x, lo_y)
z2 = karatsuba(hi_x, hi_y)
z1 = karatsuba(lo_x + hi_x, lo_y + hi_y) - z0 - z2
return (z2 << (2 * m)) + (z1 << m) + z0
print(karatsuba(1234, 5678) == 1234 * 5678) # True
for _ in range(1000):
x = random.getrandbits(random.randint(1, 512))
y = random.getrandbits(random.randint(1, 512))
assert karatsuba(x, y) == x * y
print("all random tests passed")The base case hands small operands to the built-in multiplication, which stands in for single-word hardware multiplication.
Common Errors
Splitting the Two Operands at Different Points
Both numbers must be split at the same m. The formula depends on both high halves being scaled by the same B^m. If you split x at its own midpoint and y at a different one, the cross terms land at the wrong powers and the result is wrong. The code above picks m from the longer operand and uses it for both.
Worrying About the Sums Being Longer
The operands a + b and c + d can be one digit (or one bit) longer than the halves. This does not break the analysis. A subproblem of size n/2 + 1 still gives O(n^(log₂ 3)). It does matter in fixed-width implementations, where the carry needs room. In C, an extra bit can overflow a word.
Subtracting in the Wrong Order
z₁ must be computed in full before it is shifted. If you shift (a + b)(c + d) and then subtract unshifted z₀ and z₂, the result is wrong. With unsigned arithmetic, the intermediate value (a + b)(c + d) − z₂ is always non-negative because it equals ad + bc + bd. Some variants compute (a − b)(d − c) instead to keep operands small, and those must handle negative values.
Negative Inputs
The function above assumes non-negative integers. For signed inputs, multiply the absolute values and fix the sign afterward. Python's >> on negative numbers rounds toward negative infinity, so the masking trick gives unexpected halves.
Trade-offs in Practice
The base case x < 10 or y < 10 in the code above is far too small for speed. Pure-Python Karatsuba will also never beat Python's built-in *, because that is written in C and already uses Karatsuba for large inputs. The code shows how the algorithm works and is not meant as a replacement.
Karatsuba is the first step in a family of methods. Toom–Cook splits each number into k parts and uses 2k − 1 products, which pushes the exponent closer to 1. FFT-based methods such as Schönhage–Strassen reach O(n log n log log n). Harvey and van der Hoeven's 2019 algorithm reaches O(n log n), though only for astronomically large inputs. Libraries like GMP pick among these by operand size.
The general lesson for divide and conquer is that the number of subproblems a sets the exponent in case 1 of the master theorem. Cutting a by one at the cost of extra linear-time combine work pays off once n is large enough.
Exercise
Use Karatsuba with base 10 and m = 1 to compute 47 × 83. Give z₂, z₀, z₁, and the final result.
Show answer
a = 4, b = 7, c = 8, d = 3. z₂ = 4 × 8 = 32. z₀ = 7 × 3 = 21. (a + b)(c + d) = 11 × 11 = 121, so z₁ = 121 − 32 − 21 = 68. Result: 32 × 100 + 68 × 10 + 21 = 3200 + 680 + 21 = 3901, which equals 47 × 83.
Exercise
Suppose a scheme splits each number into three parts and multiplies them using 5 recursive products of size n/3, plus O(n) combine work. What is its running time, and how does it compare with Karatsuba?
Show answer
The recurrence is T(n) = 5T(n/3) + O(n). Since log₃ 5 ≈ 1.465 > 1, case 1 of the master theorem gives O(n^(log₃ 5)) ≈ O(n^1.465). That is asymptotically faster than Karatsuba's O(n^1.585). This is Toom-3, a Toom–Cook method, which has larger constant factors.
Exercise
Why does the obvious split into four half-size products give no asymptotic improvement over schoolbook multiplication?
Show answer
Its recurrence is T(n) = 4T(n/2) + O(n). Because n^(log₂ 4) = n² dominates the linear combine cost, T(n) = O(n²), the same as schoolbook. The recursion does the same digit-by-digit products in a different order. The speedup comes only from cutting the number of subproblems.