Project Nayuki


Karatsuba multiplication

Multiplying two \(n\)-bit integers with the naive algorithm takes \(O(n^2)\) time. But it can be done faster – with the Karatsuba algorithm it takes \(O(n^{\log_2 3}) \approx O(n^{1.58})\) time, which gives a significant speed-up for large numbers.

The Karatsuba multiplication algorithm for integers \(x\) and \(y\) is based on the following observations:

  1. Select a modulus \(m \in \mathbb{N}^+\). Any number would work, but it’s most efficient to choose a power of 2 that is near \(\sqrt{x}\).
  2. Let \(x_\text{low} = x \text{ mod } m\) and \(x_\text{high} = \lfloor x / m \rfloor\). We have \(x = m x_\text{high} + x_\text{low}\).
  3. Let \(y_\text{low} = y \text{ mod } m\) and \(y_\text{high} = \lfloor y / m \rfloor\). We have \(y = m y_\text{high} + y_\text{low}\).
  4. Let \(a = x_\text{high} y_\text{high}\).
  5. Let \(b = (x_\text{low} + x_\text{high})(y_\text{low} + y_\text{high})\).
  6. Let \(c = x_\text{low} y_\text{low}\).
  7. Then \(xy = am^2 + (b - a - c)m + c\).

Benchmark

Benchmark graph

The time to multiply two n-bit integers with naive multiplication versus Karatsuba multiplication was measured and graphed. For example, for n = 107 bits, naive multiplication took 1079 seconds while Karatsuba multiplication took 39 seconds, implying a 28× speed-up. Karatsuba multiplication starts to be faster than naive multiplication at around n = 3000 bits.

The tests were performed on Intel Core 2 Quad Q6600 (2.40 GHz) using a single thread, Windows XP SP 3, Java 1.6.0_22.

Source code

More info