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
- 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.
- 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).
- Recombine: x·y = ac·10²ᵐ + (ad + bc)·10ᵐ + bd.
- Applying this recursively gives O(n^1.585) instead of O(n²).
Watch it run
Loading animation…