0% found this document useful (0 votes)
17 views

Fast Product: Solution1 - Using Shift Operator

The document discusses two algorithms for fast integer multiplication: 1. Using shift operators, which performs multiplication in O(x) time where x is the number of bits, by shifting and adding bits. 2. Using divide and conquer, which breaks the numbers into halves, recursively calculates partial products, and recombines them in O(n^1.59) time using additions and subtractions rather than multiplications. Pseudocode is provided for the divide and conquer algorithm.

Uploaded by

sadiya bhawania
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
17 views

Fast Product: Solution1 - Using Shift Operator

The document discusses two algorithms for fast integer multiplication: 1. Using shift operators, which performs multiplication in O(x) time where x is the number of bits, by shifting and adding bits. 2. Using divide and conquer, which breaks the numbers into halves, recursively calculates partial products, and recombines them in O(n^1.59) time using additions and subtractions rather than multiplications. Pseudocode is provided for the divide and conquer algorithm.

Uploaded by

sadiya bhawania
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2

2.

Fast Product
-I've written two ways to solve the fast product solution.

1. Using shift operator


2. using divide and conquer

below is for log(n) or O(n) read more and write the answer for it

https://www.geeksforgeeks.org/multiplication-two-numbers-shift-operator/

https://stackoverflow.com/questions/9844282/what-is-the-time-complexity-of-this-multiplication-
algorithm

solution1 - using shift operator.


i'm assuming n steps of multiplication for n steps of number.
we are using 1-bits shifts and binary additions to construct the product. Shift operations and
addtions are O(1). this is a long multiplication, using 1 bit at a time.
But since we are using O(1) operations for n, an x bit number, the time complexity is O(x), where
x is the number of bits in the number.

In [4]: #n = multiplicand and m = multiplier


def binaryMultiplication(n, m):
product = 0
while(m):
if m & 1 :
product += n
n <<=1
m >>=1
return product

In [5]: binaryMultiplication(1001,101)

Out[5]: 101101

solution 2 - divide and conquer


the basis of the algorithm is breaking up the digits of a number and recombining them in a way
that allows perform large number of multiplications using a small number of additions and
substractions.
This method saves time because addition take only 2n steps as opposed to n^2 steps.
for fast product, we can divide 2 integers into left and right.

assuming two integers to be A and B. A = AL 2^(n/2) + AR [AL and AR contain left most and
rightmost n/2 bits of A] B = BL 2^(n/2) + BR [BL and BR contain left most and rightmost n/2 bits of
B]
product of the above two algos can be written as A B = (AL 2^(n/2) + AR)(BL * 2^(n/2) + BR) ....1

= 2^n ALBL + 2^(n/2)(ALBR + XRBL) + ARBR ....2

= 2^n ALBL + 2^(n/2)[(AL+AR)(BL+BR) - ALBL - ARBR)] + ARBR....3

= 2^(2ceil(n/2)) ALBL + 2^(ceil(n/2)) * [(AL + AR)(BL + BR) - ALBL -


ARBR] + ARBR .... 4

till step 2, we divide the algo into 4 equal parts of size n/2, but despite that the complexity remains
O(n^2).

we need to breakup the multiplication taking place in the middle terms to additons and
substractions.

step 4: ceil is added to handle odd cases.

after doing so the recurrence relation is:: T(n) = 3 T(n/2) + O(n) and solution of the recurrence is
O(n^1.59)

Psuedocode - function divideAndconquerCalc(num1, num2): 1.if we have 1 digit numbers multiply


them together. if len(str(num1)) ==1 or len(str(num2)) == 1: return (num1* num2)

2.calculating the size of number of handle even and odd cases n = max(len(str(num1)),
len(str(num2))) m = n/2

3.split the numbers into two parts, we can use any base here. I'm using 2 since we will be calculating
binary numbers num1R, num1L = split(num1/2(m), num1 % 2(m)) num2_R,num2_L =
split(num2/2(m), num2 % 2(m))

4.Use recursion to calculate the sub-parts. r1 = divideAndconquerCalc(num1_R,num2_R) r2 =


divideAndconquerCalc(num1_L,num2_R) r3 = divideAndconquerCalc(num1_R+num1_L,
num2_R+num2_L) - r1 -r2

5.calculate the product- prod = r1 2**(m 2) + (r3 * 2 ** m) + r2

You might also like