The Karatsuba algorithm multiplies two numbers quickly by employing a divide-and-conquer strategy.
Arthur Benjamin performs a spectacular trick: he multiplies two numbers in his head! By performing his trick out loud, he also lets us in on his secret: there are no secrets. He is very good at mental arithmetic. His method is a straightforward application of the elementary school identity:
A careful examination of his videos reveals that he is using a shortcut. He does multiply two different 2-digit numbers. However, when he does the trick for 3,4, or 5-digit numbers, he only squares them. Multiplying two unequal 4-digit numbers is quite hard using his method. He has to split each number into two 2-digit numbers and performs four multiplications of two 2-digit numbers. The final answer will be produced by doing shifts and additions. By choosing to square, he can cut the number of calculations drastically.
Let’s carefully examine his method and see how we can tackle the problem of multiplying two 4-digit numbers in our heads. Let’s first look at the general representation. We are given a -digit number, , by specifying its digits . We let denote its representation and denote its value. Thus, we have the following:
Typically, when writing a concrete number, we dispense with parentheses and commas. We write instead of . A 4-digit number can be split into two 2-digit numbers, by setting and where consists of the higher two digits and consists of the lower two digits. Hence . For example, if then and . Similarly, if then and . The product of two numbers and is given by:
In our example, the product will be as follows:
To perform this calculation, we’ll compute and Now we can calculate by performing some shifts (which are multiplications by powers of ten) and additions. For this trick to work, we must be able to multiply two 2-digit numbers in our heads and be good at shifting and adding.
Let’s see what happens if we square a number, as Prof. Benjamin does. To compute we have,
In our example,
This reduces our work considerably. We have performed three multiplications instead of four. But it is better than that: two of the multiplications are squarings. It seems that Prof. Benjamin has memorized all 2-digit squares. Hence, the only difficult part for him is to do a 2-digit multiplication; that is, compute
We’ll have to develop the following skills to perform the trick:
Many of our friends may agree that Step 1 is certainly doable. There are only 100 squares to remember and there are many bright people who don’t find this task very challenging. It is fun to read another paper by Prof. Benjamin on squaring and even try to develop our own shortcuts to acquire this skill.
Step 2 is the hardest. Brute force memorization is nearly impossible as the number of 2-digit pairs is 10,000. Therefore, it is wise to compute this step. Prof. Benjamin is very good at this; in fact, he can perform a multiplication of a 2-digit number with a 3-digit number in his head. This allows him to pull off his spectacular trick of squaring a 5-digit number in his head.
We learn to multiply numbers in elementary school. The algorithm that we use is ancient and was widely used by Sumerians and Egyptians for at least four thousand years. Karatsuba suggests that “there is every reason to believe that it has existed for at least six millennia.”
To multiply a number with we multiply with each digit of thereby calculating a row of our computation. Each row is appropriately shifted by adding zeros. The task is completed by adding the rows. The amount of work to compute a row is proportional to the number of digits in . Hence the total amount of work just to compute the rows is equal to the number of digits in times the number of digits in . If both and are -digit numbers then the total work required is (at least) proportional to .
Some variations are used in different cultures to teach this algorithm. However, none of these variations reduces work. They essentially make it easier for children to learn the method and indicate some useful shortcuts.
A possible approach to find a faster algorithm is to use the divide and conquer paradigm. Suppose is even and we wish to multiply two -digit numbers and . Let (respectively ) be the numbers formed by taking high digits and low digits of (respectively ). A generalization of Equation (1) gives us:
This leads to a recursive divide-and-conquer algorithm. This equation converts the problem of multiplying two -digit numbers to four problems of multiplying two -digit numbers. Once the four subproblems have been solved the required value is computed by doing some shifts and additions. These shifts and additions are easier as they require work. Unfortunately, this algorithm is no better than the classical method for multiplying. Multiplying two -digit numbers this way also leads to an algorithm. Our first attempt to improve the classical algorithm is a failure.
Since all known algorithms for multiplication required time, the great mathematician Andrey Kolmogorov suggested in 1956, that mathematicians try to prove that an improved algorithm is impossible. His -conjecture was perhaps based on the idea that “if a more economical method existed, it would have been already found”. After all, 6000 years have produced many brilliant minds.
Karatsuba, a 23-year old student, heard about the Kolmogorov -conjecture from him, at a conference in Moscow State University in 1960. He decided to try exactly the opposite and attempted to find a faster algorithm! Within a week he showed Kolmogorov that a fundamentally faster algorithm was possible.
Karatsuba’s main idea is to somehow reduce the number of subproblems. He realized that if he uses subtractions then it is possible. Karatsuba computes , and, the Karatsuba quantity,
Computing requires us to add the high digits of to its lower digits (and the same for ). This may seem very unusual (and it certainly is) as we add the high and low digits of one number without looking at the digits of the other number!
Observe that,
and to our delight we realize that and therefore, we have
Thus the problem of multiplying two -digit numbers has been converted to the problem of computing and : only three smaller multiplications involving numbers (approximately) half the size. The product can now be calculated by performing some subtractions, additions, and shifts.
Equation (2) can be repeated to amplify these dividends. At the second level of recursion there are only subproblems instead of , at the third level there are only subproblems instead of and so on. Hence two -digit numbers can be multiplied by doing operations instead of operations used by the classical method. Only steps are needed for multiplying two -digit numbers. This is considerably faster than steps, needed for the classical method, as
Karatsuba’s idea is truly revolutionary. His audacity as a 23-year old student to try exactly the opposite of what a legend had suggested, coupled with his use of subtraction is what we call “negative thinking.” All students should practice Karatsuba’s approach: mathematics and science progress by challenging the expert opinion.
Here, we do want to emphasize that denying a mathematical theorem, whose clear proof is available, or denying a well-established scientific theory without any evidence is not only unproductive but detrimental for the mental health of students.
In a week’s work, Karatsuba had improved upon the classical method that had reigned for thousands of years. Not only that, he had broken a psychological barrier. He showed the mathematical world that the problem of multiplying two -digit numbers is a fertile area of research.
Initially, Kolmogorov was “agitated” at Karistuba’s discovery. But, mathematical truths are undeniable and soon Kolmogorov started lecturing about Karatsuba’s method and popularized it. After a couple of years, Karatsuba received a reprint of a paper he had never written! The paper contained his method and gave full credit to him. It also contained a result of another mathematician Ofman. The only authors of the paper were Karatsuba and Ofman, with proper credit given to each respective author -Kolmogorov’s name did not appear as an author.
The story of Karatsuba and Kolmogorov is extremely moving in today’s competitive world. Kolmogorov must have realized that Karatsuba had changed the course of history on a fundamental problem like multiplication. Despite his initial agitation, he acted conscientiously as a great promoter of mathematics. This story is a timeless tale of a young mathematician teaching a legend and the legend accepting to learn.
Karatsuba’s idea can be generalized to obtain an -time algorithm for any The mathematical community gained confidence and another breakthrough lead to the development of an algorithm that uses the Fourier Transform. More improvements followed leading to an algorithm that requires time by Harvey and van der Hoeven.
is the inverse of the tower function, , which is defined as: and . is the smallest such that .
Recently, they have developed an -algorithm. Many computer scientists believe that we have reached the ultimate time complexity for this problem.
Karatsuba’s method requires us to perform subtractions. Most people are far more comfortable with adding numbers than they are with subtracting them. This bias does not have a sound scientific basis: algorithms for subtractions are very similar to those for adding numbers. Those who are familiar with 2’s complement know that computers perform addition and subtraction using the same hardware. Thus, learning to subtract quickly is as easy as learning to add: we just have to practice. With this observation in mind, we apply Karatsuba’s method to the problem of squaring 4-digit number in our head. The Karatsuba quantity is given by
and we have,
Look at what we have achieved: we have dispensed with the problem of multiplying altogether! We have to compute and the Karatsuba quantity which itself is a square. So, what are the skills needed to pull off this trick?
This method has a slight advantage of dispensing with the doubling also. Now, and are only 2-digit numbers for us. However, the Karatsuba quantity is a sum of two 2-digit numbers and can be as large as 198. So, we will have to memorize the squares of numbers up to . That’s a bummer! Can we reduce the work and not overload ourselves? The answer is yes. Let us define the modified Karatsuba quantity, , by squaring the difference of and . We have,
We now have a lovely equation:
We can base our method on this equation and the skills needed to perform a 4-digit squaring in our head are:
Let’s assume we have all the 2-digit squares memorized and work out a concrete example by squaring . We have and Now we recall their squares from the memory.
From these three squares we compute:
The final answer is computed by adding these quantities with appropriate shifts:
We are fairly confident that many people are capable of doing this feat by using the Karatsuba shortcut. In fact, we would not be surprised to see Prof. Benjamin square a 6-digit number in his head. He seems to remember (or compute them fast enough to create that impression) all the squares of 3-digit numbers. With the Karatsuba’s trick at his disposal, it would be interesting to see what he can achieve. Perhaps, the only reason we might not see Prof. Benjamin squaring a 7 or 8-digit number in his head is because, he is involved in doing other work.
An interesting question arises by studying the problem above. Is it strictly easier to square an -digit number than multiply two arbitrary -digit numbers? After all, using Karatsuba’s method, we were able to reduce the work considerably. Yet, the answer in general (or for large numbers) is no. Observe that:
This equation tells us that if we could develop a fast method for squaring, then it can be used to multiply two different numbers fast also. Equation (3) serves to prove a negative fact: the seemingly simpler task of squaring is at least as hard as multiplying two different numbers.
Despite this, Equation (3) has a positive application in mathemagic. Suppose Prof. Benjamin decides to multiply two arbitrary 3-digit numbers. He can compute , which is a squaring of a 3-digit number and he is very good at it. He also has to compute , which might be a 4-digit number. Although he is also capable of squaring a 4-digit number with some effort, we note that this 4-digit number is at most . This fact can allow him to speed up that calculation too. He will have to subtract and then divide the answer by . We believe with a little practice he can accomplish this feat as well.
This blog highlighted how Karatsuba’s method reduces so much effort in squaring a number in the head. It will be interesting to see what improvements this approach and other similar observations bring to mathemagic. We can only wait and see what new feats the future mathemagicians can accomplish.
Free Resources