Given two big integers represented as strings, Multiplication them and return the production as string.
For example, given a=2343324 and b=232232 then retiurn c = a*b = 23433242334323342 * 23223233232434324 = 544195652122144709711313995190808
We can see that the problem is trivial if we had given two small integers. But as the numbers can be really big we can’t fit the operands or the results into primitive data types. So, we need to go back to old school process where we do the multiplication of two numbers by multiplying one number with each digit of another and shift the subsequent results to left by 1 digit, and then add all these intermediate results for each digit of second number.
For example, let’s consider a = 231 and b = 51. Then the elementary process of multiplication would be –
231 51 ---------- 231 <--- 231 * 1 = 231 1155x <--- 231 * 5 = 1155, then shift left by 1 position is equivalent to padding one zero to right i.e. 11550 ----------- 11781 <--- adding 231+11550 = 11781
Basically, we need to take each digits from right to left of the second number and multiplying with the first number. For each digit in second number from right to left there will be one intermediate prod. We need to pad one additional zero to the end of subsequent intermediate results.
Now question is how to avoid overflow when multiplying first number with each of the digits of second number? For example, 23433242334323342 multiply by 4 certainly can't be done in integer or any other primitive type arithmetic. The solution is again in the old school way of multiplication. That means, from right to left, for each single digit in second number we multiply each single digit of first number and store the multiplication digit with extra digit storing as carry to be added in next digit multiply result.
Now we have a list of intermediate results that we need to add. Adding large numbers also suffers from overflow issue , so we also need to do this addition in old school way, i.e. add digit to digit at each position and push the carry forward.
The ld school algorithms are straightforward but the details is in the implementation. We need to make sure we construct, append zeros, and then store intermediate results efficiently so that we can do the final addition efficiently. Below is the implementation of the algorithm using stringbuffer construct. It runs in O(nm) where n=number of digits in first number and m=number of digits in second number.