Week 3 - Fast Integer multiplication
Multiplying
-bit integers problem Given
-bit integers and what is ?
Naïve approaches will do this in
Gauss’s trick (aside)
Suppose have two complex numbers
Multiplication of real numbers uses
Looking at the expansion
Naïve divide and conquer approach
Suppose we have
EasyMultiply(x,y):
Input: n=2^k-bit integers x & y
Output xy
if k = 1:
return xy
else:
X_L = 1st 2^(k-1)-bits of x, X_R = Last 2^(k-1)-bits of x
Y_L = 1st 2^(k-1)-bits of y, Y_R = Last 2^(k-1)-bits of y
A = EasyMultiply(X_L, Y_L), B = EasyMultiply(X_R, Y_L)
C = EasyMultiply(X_L, Y_R), D = EasyMultiply(X_R, Y_R)
return 2^nA + 2^(2^k-1)(B + C) + D
To analyse the run time of this algorithm not the split and shift operations take
Better approach
This time we combine Gauss’s trick with the divide and conquer approach to speed up this algorithm.
Suppose we have
EasyMultiply(x,y):
Input: n=2^k-bit integers x & y
Output xy
if k = 1:
return xy
else:
X_L = 1st 2^(k-1)-bits of x, X_R = Last 2^(k-1)-bits of x
Y_L = 1st 2^(k-1)-bits of y, Y_R = Last 2^(k-1)-bits of y
A = EasyMultiply(X_L, Y_L), B = EasyMultiply(X_R, Y_R)
C = EasyMultiply(X_L + X_R, Y_L + Y_R)
return 2^nA + 2^(2^k-1)(C - A - B) + B
This works slightly faster with