Search⌘ K
AI Features

Fast Multiplication

Explore the Karatsuba algorithm for fast multiplication of large numbers by applying recursion and divide-and-conquer strategies. Understand how splitting numbers and reducing recursive calls improve performance compared to naive multiplication, and gain insight into the algorithm's significance in computational complexity.

The divide-and-conquer algorithm

Two ancient algorithms for multiplying two nn-digit numbers in O(n2)O(n^2) time are: the grade-school lattice algorithm and the Egyptian peasant algorithm. Maybe, we can get a more efficient algorithm by splitting the digit arrays in half and exploiting the following identity:

(10ma+b)(10mc+d)=102mac+10m(bc+ad)+bd(10^ma + b)(10^mc + d) = 10^{2m}ac + 10^m(bc + ad) + bd

This recurrence immediately suggests the following divide-and-conquer algorithm to multiply two nn-digit numbers, xx and yy. Each of the four subproducts ac,bc,ad,ac, bc, ad, and bdbd is computed recursively, but the multiplications in the last line aren’t recursive because we can multiply by a power of ten by shifting the digits to the left and filling in the correct number of zeros, all in O(n)O(n) time.

Algorithm


SplitMultiply(x,y,n):if n=1return xyelsemn/2ax/10m; bx mod 10m〈〈x=10m a+b〉〉cy/10m; dy mod 10m〈〈y=10m c+d〉〉eSplitMultiply(a,c,m)fSplitMultiply(b,d,m)gSplitMultiply(b,c,m)hSplitMulti ...