Skip to content
StackDecode
Divide & conquerAnimated

Karatsuba Multiplication

Multiply big numbers with 3 half-size products instead of 4.

Time

O(n^1.585)

Extra space

O(n)

Use it for

Big-integer arithmetic.

How it works

  1. Schoolbook multiplication of two n-digit numbers needs 4 half-size multiplications after splitting x = a·10ᵐ + b and y = c·10ᵐ + d: ac, ad, bc, bd.
  2. Karatsuba notices that ad + bc = (a + b)(c + d) − ac − bd, so only 3 half-size multiplications are needed: ac, bd and (a + b)(c + d).
  3. Recombine: x·y = ac·10²ᵐ + (ad + bc)·10ᵐ + bd.
  4. Applying this recursively gives O(n^1.585) instead of O(n²).

Watch it run

Loading animation…