The Karatsuba Integer Multiplication Algorithm is a really simple,fast recurrence based method to multiply two digit numbers.It was discovered by Anatoly Karastuba in 1960 and published in 1962. It is a good algorithm to start out within the Divide and Conquer algorithm and recursion for a beginner.
So why Karastuba?
Before we get into that question, let's focus/recap on some basic concepts.
A primitive operation is an operation of a the fundamental nature. It can be intuitively realised that these are namely two:- addition and multiplication. It can be easily realised that the larger the number of primitive operations( especially multiplication ) that are needed to be performed to produce an output, the more time it takes.
Now, consider a multiplication of two digit numbers, say and
.
It can be seen that to find a product and of these two numbers, we have two perform primitive multiplications accompanied with some primitive additions. It can now be realised that for any two digit numbers, the maximum primitive multiplications you need to carry out is which is considerable amount of manual work for large values of . Can we do better than this?
Well, we actually can using the Karatsuba Integer Multiplication Algorithm.
The Algorithm
Step 1 :- Write the numbers and which are to multiplied in the form :-
and
where is the number of digits in the number or . If is an odd number say , you can raise to the power as an optimal value, however any works just fine.
Step 2:-
Step 3:- Recursively calculate and if the multiplicands are of a decent order say . You
might want to use the Grade school algorithm instead here if the order is less than that.
Step 4:- Recursively calculate (again if the order is decent enough) and then subtract and from it. This saves you from an extra work of calculating and seperately. Now do the remaining addition to get your product.
Now for our example, we consider the same multiplication prior stated i.e.:- of and .
Let , , and . here =
Step 1 :- = and =
Step 2 :- = . Subtract and =
Step 3 :- = =
Note that Karatsuba Integer Multiplication Algorithm only works on positive integers and if you want to implement it for negative numbers, you will need to make some obvious tweaks.
Easy Math Editor
This discussion board is a place to discuss our Daily Challenges and the math and science related to those challenges. Explanations are more than just a solution — they should explain the steps and thinking strategies that you used to obtain the solution. Comments should further the discussion of math and science.
When posting on Brilliant:
*italics*
or_italics_
**bold**
or__bold__
paragraph 1
paragraph 2
[example link](https://brilliant.org)
> This is a quote
\(
...\)
or\[
...\]
to ensure proper formatting.2 \times 3
2^{34}
a_{i-1}
\frac{2}{3}
\sqrt{2}
\sum_{i=1}^3
\sin \theta
\boxed{123}
Comments
This is wrong for two reasons. First, you will eventually need to compute single-digit by single-digit, which is not possible with Karatsuba's algorithm. (Remember that any recursion needs a base case.) Second, even if not, you will probably still want to use it for small enough a,b,c,d, because Karatsuba's algorithm needs more additions than schoolbook method, and when a,b,c,d are small, additions and multiplications are roughly the same cost. Wikipedia says you want to use Karatsuba's algorithm only when the operands are at least 320 bits long, so when a,b,c,d are just, say, 10digits (1010≈233), you will want to use schoolbook method.
Log in to reply
Thanks. I'm still learning though and I wanted to just share a bit about it. That was a bit of my interpretation which obviously is wrong. I will do the needful changes.
That's nice. What is the resultant complexity of the algorithm?
Log in to reply
I'm new to algorithmic analysis so I might not be the best person to explain that. Hopefully you might find this useful Karatsuba Multiplication.
This idea extends to matrix multiplication, where the result is more surprising. In particular, it shows that matrix multiplication need not be or order n3.