Fast Multiplication

Understand the efficiency and effectiveness of the fast multiplication algorithm.

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 sub-products ac,bc,ad,ac, bc, ad, and bdbd is computed recursively, but the multiplications in the last line are not 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)hSplitMultiply(a,d,m)return 10 ...

Create a free account to access the full course.

By signing up, you agree to Educative's Terms of Service and Privacy Policy