Mathwords logoMathwords

Karatsuba Algorithm — Definition, Formula & Example

The Karatsuba algorithm is a fast way to multiply two large numbers. It splits each number in half and finds the answer with three smaller multiplications instead of the four that schoolbook multiplication needs.

Let xx and yy be nn-digit integers in base BB. Write x=aBm+bx = aB^m + b and y=cBm+dy = cB^m + d with m=⌈n/2⌉m = \lceil n/2 \rceil and 0≤b,d<Bm0 \le b, d < B^m. Karatsuba's method computes acac, bdbd, and (a+b)(c+d)(a+b)(c+d) recursively. It then uses the identity ad+bc=(a+b)(c+d)−ac−bdad + bc = (a+b)(c+d) - ac - bd to assemble xyxy. The running time satisfies T(n)=3T(n/2)+O(n)T(n) = 3T(n/2) + O(n), so T(n)=O(nlog⁡23)≈O(n1.585)T(n) = O(n^{\log_2 3}) \approx O(n^{1.585}). Schoolbook multiplication takes O(n2)O(n^2).

Key Formula

xy=ac B2m+[(a+b)(c+d)−ac−bd]Bm+bdxy = ac\,B^{2m} + \big[(a+b)(c+d) - ac - bd\big]B^m + bd
Where:
  • x,yx, y = The two numbers being multiplied
  • a,ca, c = The high halves of x and y
  • b,db, d = The low halves of x and y
  • BB = The base of the number system (10 for decimal, 2 for binary)
  • mm = The number of digits in each low half

Worked Example

Problem: Use the Karatsuba algorithm to compute 1234×56781234 \times 5678 with base B=10B = 10 and split point m=2m = 2.
Step 1: Split each number into a high half and a low half of two digits each.
1234=12⋅102+34,5678=56⋅102+781234 = 12 \cdot 10^2 + 34, \qquad 5678 = 56 \cdot 10^2 + 78
Step 2: Compute the three products. Notice that you never compute adad or bcbc separately.
ac=12⋅56=672,bd=34⋅78=2652,(a+b)(c+d)=46⋅134=6164ac = 12 \cdot 56 = 672, \quad bd = 34 \cdot 78 = 2652, \quad (a+b)(c+d) = 46 \cdot 134 = 6164
Step 3: Recover the middle coefficient by subtraction, then shift the pieces and add.
6164−672−2652=2840,672⋅104+2840⋅102+2652=7,006,6526164 - 672 - 2652 = 2840, \qquad 672 \cdot 10^4 + 2840 \cdot 10^2 + 2652 = 7{,}006{,}652
Answer: 1234×5678=7,006,6521234 \times 5678 = 7{,}006{,}652. Karatsuba used 3 smaller multiplications where the schoolbook split needs 4.

Why It Matters

Karatsuba's 1960 result was the first proof that multiplying nn-digit numbers takes fewer than n2n^2 steps. It is the standard first example of divide-and-conquer recurrences in an algorithms course. Big-number libraries such as GMP and Python's integer arithmetic switch to Karatsuba once numbers reach a few dozen machine words, because RSA cryptography and high-precision computation multiply huge integers constantly.

Common Mistakes

Mistake: Using (a+b)(c+d)(a+b)(c+d) directly as the middle coefficient, which gives 61646164 instead of 28402840 in the example.
Correction: The product (a+b)(c+d)(a+b)(c+d) expands to ac+ad+bc+bdac + ad + bc + bd. You must subtract both acac and bdbd to isolate ad+bcad + bc. Also shift by B2mB^{2m} and BmB^m before adding, not by BmB^m for both terms.

Related Terms

  • Standard Algorithm — Schoolbook method; split in halves it needs four multiplications, not three
  • Multiplication — The basic operation Karatsuba performs faster for large numbers
  • Algorithm — General idea of finite step-by-step procedures, which Karatsuba exemplifies
  • Multiply — The basic operation that Karatsuba speeds up for large numbers