Introduction

Typically multiplying two n-digit numbers require n2 multiplications. That is actually how we, humans, multiply numbers. Let’s take a look of an example in case we’ve to multiply two 2-digit numbers.

12 x 15= ?

OK, we know that the answer is 180 and there are lots of intuitive methods that help us get the right answer. Indeed 12 x 15 it’s just a bit more difficult to calculate than 10 x 15, because multiplying by 10 it really easy – we just add one 0 at the end of the number. Thus 15 x 10 equals 150. But now again on 12 x 15 – we know that this equals 10 x 15 (which is 150) and 2 x 15, which is also very easy to calculate and it is 30. The result of 12×15 will be 150 + 30, which fortunately isn’t difficult to get and equals to 180.

That was easy but in some cases the calculations are a bit more difficult and we need a structured algorithm to get the right answer. What about 65 x 97? That is not so easy as 12 x 15, right?

The algorithm we know from the primary school, described on the diagram below, is well structured and help us multiply two numbers.

We see that even for two-digit numbers this is quite difficult – we have 4 multiplications and some additions.

We need 4 multiplications in order to calculate the product of two 2-digit numbers!

However so far we know how to multiply numbers, the only problem is that our task becomes very difficult as the numbers grow. If multiplying 65 by 97 was somehow easy, what about

374773294776321
x
222384759707982

It seems almost impossible.

History

Andrey Kolmogorov is one of the brightest russian mathematicians of the 20th century. In 1960, during a seminar, Kolmogorov stated that two n-digit numbers can’t be multiplied with less than n2 multiplications!
Only a week later a 23-year young student called Anatolii Alexeevitch Karatsuba proved that the multiplication of two n-digit numbers can be computed with n ^ lg(3) multiplications with an ingenious divide and conquer approach.

Overview

Basically Karatsuba stated that if we have to multiply two n-digit numbers x and y, this can be done with the following operations, assuming that B is the base of and m < n.

First both numbers x and y can be represented as x1,x2 and y1,y2 with the following formula.

Now the thing is that 11 * 15 it’s again a multiplication between 2-digit numbers, but fortunately we can apply the same rules two them. This makes the algorithm of Karatsuba a perfect example of the “divide and conquer” algorithm.

Implementation

Standard Multiplication

Typically the standard implementation of multiplication of n-digit numbers require n2 multiplications as you can see from the following PHP implementation.

Complexity

Assuming that we replace two of the multiplications with only one makes the program faster. The question is how fast. Karatsuba improves the multiplication process by replacing the initial complexity of O(n2) by O(nlg3), which as you can see on the diagram below is much faster for big n.

O(n^2) grows much faster than O(n^lg3)

Application

It’s obvious where the Karatsuba algorithm can be used. It is very efficient when it comes to integer multiplication, but that isn’t its only advantage. It is often used for polynomial multiplications.

Thanks a million for this article; it is very simple, understandable and well written. I Needed to learn this as part of my Algorithms course and you really simplified the Karatsuba method for me. Thank you very much

Good Article. True. But I was looking for fast algorithm of multiplication for big integers that may be 1522456*4^100 000 000… in size. So this algorithm multiplies the job of CPU instead of simplifying

Hey that’s a very good article which you wrote there.
Thanks a lot.
I habe only a question, to say it better, a problem executing your code on PHP.
I dont’t get the correct result and i can not figure it out why. First of i got a fatal error because the function “sum()” does not exists. I implemented it but i dont know how exactly, becuase the parameters which are loaded in the sum() function are arrays.
If i got this right, we have to sum alla array-values and the the 2 sums again, and return it.
I hope this is still reading someone and could help me.

Thanks for the article, it is really well explained with great examples.
But I tried implementing in java based on your code for PHP but you are considering different length for X and Y variable while dividing into two parts.
It was not working for me with different size(xlen/2, ylen/2) of X and Y so I tried with the min value of (xlen/2, ylen/2), and then its working fine.
Could you please tell me why this is not working in case of your logic.