Fast Product: Solution1 - Using Shift Operator
Fast Product: Solution1 - Using Shift Operator
Fast Product
-I've written two ways to solve the fast product solution.
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
In [5]: binaryMultiplication(1001,101)
Out[5]: 101101
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
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.
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)
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))