How Karatsuba multiplies with three half-size products

Short answer: Karatsuba multiplication multiplies two n-digit numbers with three multiplications of n/2 digits instead of four. Split x = x1·B + x0 and y = y1·B + y0. The classical cross terms x1·y0 and x0·y1 are recovered from the single product (x1+x0)(y1+y0) by subtracting the two squares x1·y1 and x0·y0. The recurrence is O(n^{log₂ 3}), about O(n^1.585). Below a cutoff, schoolbook multiplication is faster.


What the problem actually asks

Schoolbook multiplication forms four products of the halves. The middle coefficient of the product is the sum of the two cross terms, and that sum can be computed without forming each cross term alone. The algebra costs a few additions, which are linear, and saves one multiplication, which is the expensive branch of the recurrence.

The split radix B is a power of the base, often a power of two so the shift is free. The sums x1+x0 can have one extra digit. The recursive call has to allow that extra digit or the implementation overflows the half-size buffer.


What the algorithm guarantees

The product equals the schoolbook product. The depth of recursion is log n, and the work obeys T(n) = 3T(n/2) + O(n), whose solution is Θ(n^{log₂ 3}). Extra memory is the temporary sums and the recursive buffers. A cutoff around a few dozen machine words, tuned by measurement, switches to schoolbook multiplication. Running the recursion down to one digit loses to the additions.

The algorithm is not asymptotically the fastest. FFT multiplication is O(n log n) up to further log factors and wins for very large n. Karatsuba is the algorithm in the middle range, and it is the one whose three-product identity is worth knowing.


The working steps

  1. If n is below the cutoff, multiply by the schoolbook method and return.
  2. Split each input into high and low halves of n/2 digits. Let B be the place value of the split.
  3. Recursively compute z2 = x1·y1, z0 = x0·y0, and z1 = (x1+x0)·(y1+y0).
  4. The middle coefficient is z1 − z2 − z0. It may be negative. The implementation has to allow a signed temporary.
  5. The product is z2·B² + (z1 − z2 − z0)·B + z0.

The exponent log₂ 3 assumes the three recursive calls are on half the size and the additions are linear. An FFT-based product is faster past a larger threshold. A cutoff that is never applied makes Karatsuba slower than schoolbook on small inputs even though the asymptotics say otherwise. The identity is for a product of two scalars or two polynomials. Matrix multiplication’s analogous saving is Strassen.


Approaches that look simpler and fail

Approach Why it fails when the cross terms are still formed separately
Compute x1·y0 and x0·y1 by two more recursive calls That is schoolbook multiplication in recursive clothing, four half-size products, and the exponent returns to 2.
Ignore the extra digit of x1+x0 The sum can be B plus a half-size number. A half-size buffer drops the carry and the middle coefficient is wrong.
Run the recursion to single digits on a machine word The linear additions dominate. A cutoff is part of a serious implementation.
Quote O(n log n) for Karatsuba The closed form is n to the power log₂ 3. The n log n multiplication is the FFT.

Schoolbook multiplication is the cutoff. FFT multiplication is the large-n algorithm. Strassen’s algorithm is the similar saving for matrices. Karatsuba is the three-product integer or polynomial multiply.


A working example

This Python fragment multiplies with three half-size products. The middle product is z1 − z0 − z2. 1234 × 5678 equals the schoolbook product 7006652.

def karatsuba(x, y):
    if x < 10 or y < 10:
        return x * y
    n = max(len(str(x)), len(str(y))) // 2
    high = 10 ** n
    a, b = divmod(x, high)
    c, d = divmod(y, high)
    z0 = karatsuba(b, d)
    z2 = karatsuba(a, c)
    z1 = karatsuba(a + b, c + d) - z2 - z0
    return z2 * high * high + z1 * high + z0

assert karatsuba(1234, 5678) == 1234 * 5678
print(karatsuba(1234, 5678))

Where it is used

GMP multiplies medium-size integers with Karatsuba, then switches to Toom-Cook and to FFT multiplication as the operands grow. OpenSSL’s bignum code has the same ladder. Schoolbook multiplication is the base case below the Karatsuba cutoff.


When you can drop this algorithm

Drop Karatsuba below the measured cutoff and above the size where FFT multiplication wins. Keep it for the middle range of big-integer or polynomial products, and allow the extra digit in the sums.


Why this stays published

We publish this so a search for Karatsuba returns the three half-size products and the exponent log base 2 of 3. Karatsuba multiplies two n-digit numbers with three recursive half-size products, in O(n^1.585) operations, by recovering the cross terms from a single extra product.

本文由 HoHo 與 AI 協作整理,最後更新於 2026 年 11 月 13 日。

Leave a Reply

Your email address will not be published. Required fields are marked *