Loading...
Algebraic trick: replace four size n/2 multiplications with three plus linear overhead to achieve sub-quadratic complexity.
Multiply large integers faster than classical O(n^2) by reducing the count of recursive multiplications using digit split and recombine.
Represent operands in base B, splitting at midpoint m:
Classical method needs four products (x1y1, x1y0, x0y1, x0y0). Use the identity:
Compute only:
Reconstruct product:
Each level: 3 multiplications on n/2 digits + O(n) additions.
1function karatsuba(x, y):2 if small(x,y): return x*y3 split x = x1*B + x0; y = y1*B + y04 z0 = karatsuba(x0, y0)5 z1 = karatsuba((x0+x1),(y0+y1))6 z2 = karatsuba(x1, y1)7 return (z2*B^2) + ((z1 - z2 - z0)*B) + z0
T(n) = 3T(n/2) + O(n)
Master Theorem => O(n^(log2 3)) ~ O(n^1.585).
Addition/subtraction overhead linear; depth is log2 n.