Integer Multiplication#
Contents#
Conceptually, the multiplication of two integers
Divide and Conquer#
D/C 1#
INPUT two integers a and n
OUTPUT an integer an
MULT(a, n)
1 if n == 1
2 return a
3 return a + MULT(a, n - 1)
In this less common D/C approach to integer multiplication, the problem size is being reduced by one for each recursive call.
D/C 2#
INPUT two integers a, and n even
OUTPUT an integer an
MULT(a, n)
1 if n == 1
2 return a
3 s1 = mult(a, n/2) # sub problem
4 s2 = mult(a, n/2) # sub problem
5 return s1 + s2 # combine
In this common D/C approach to integer multiplication, the problem size is being reduced by half for each recursive call.
By the Master Theorem we have
This algorithm can be optimized! Since the two problems are symmetric as long as b is even, just one of the two sub problems needs to be evaluated.
INPUT two integers a, and n even
OUTPUT an integer an
MULT(a, n)
1 if n == 1
2 return a
3 s = mult(a, n/2) # sub problem
4 return s1 + s1 # combine
By the Master Theorem we have