Karatsuba Algorithm
This algorithm is from: https://www.codesdope.com/blog/article/karatsuba-algorithm/
The idea is to recursively divide an n-digit number into two halves (each half having half the digits) until they are small enough (having 1 digit) to be multiplied using the naive method. Consider two numbers X and Y having base m which are represented using the following equation,
Here, the terms having ceil(n/2) will be more clear when you will see the implementation. So for simplicity purpose, you can take n as even for now. The multiplication of X and Y is carried out in the following equations,
In the above equation, X and Y have n digits and a, b, c and d have n/2 digits each. There are four multiplications (ac, ad, bc, bd) of numbers having size n/2 (having number of digits equal to n/2). So basically, we divided our multiplication problem of size n into four sub-multiplications of size n/2. But still the time complexity of the recurrence relation formed is ). Therefore, the trick is to split the middle part of the above equation as shown below,
As the multiplications ac and bd are already calculated in the equation (1), we reduced the multiplications ad and bc into one multiplication (a+b)∗(c+d)(a+b)∗(c+d). Therefore, there are now three multiplications of size n/2 (having number of digits equal to n/2) and the recurrence relation formed is ,he resultant equation becomes as shown below,