Integer Multiplication

Integer Multiplication#


Contents#


Conceptually, the multiplication of two integers a and n can be represented as

a×n=sign(n)×(a+a++a|n|times)


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.

asub problem+MULT(a,n1)sub problemcombine

T(n)=1+T(n1)=1+1+T(n2)==1+1+1++T(1)=1+1+1++1ntimes=n=Θ(n)

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.

MULT(a,n/2)sub problem+MULT(a,n/2)sub problemcombine

T(n)=2dividesubproblems×T(b/2)conquercost per subproblem+Θ(1)combinecost of combination

T(n)=2T(n/2)+Θ(1)

By the Master Theorem we have

a=2,b=2,d=0d<logba0<1

T(n)=Θ(nlogba)=Θ(nlog22)=Θ(n)

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

T(n)=T(n/2)+Θ(1)

By the Master Theorem we have

a=1,b=2,d=0d=logba0=0

T(n)=Θ(ndlogn)=Θ(n0logn)=Θ(logn)