Problem: Given two integer numbers and , how can we multiply these numbers together while minimizing the number of single-digit multiplications?

Intuition

Product of Two Double-Digit Numbers

Suppose we’re multiplying two base 10 numbers where and are digits. Then we have the following product:

By the typical method of multiplication, we have 4 single digit multiplications!

However, using the term and through smart manipulation, we can actually reduce the number of single digit multiplications down to 3!

Plugging this back into our original equation, we find

Which now has 2 single digit multiplications, and one multiplication which may or may not be a single digit multiplication. Regardless, this product should be significantly easier than a standard 2 digit multiplication!

Thus, to calculate , we will be performing:

  • 3 single digit multiplications (approximately)
  • 6 additions or subtractions with up to 2 digits per operation
  • 3 decimal shifts (multiplication by 100 and 10)

Note that when we multiply by 10, we’re really performing a left shift (similar to bitwise shifts).

Generalization

Now, let’s try to generalize this to numbers with more digits. Suppose we’re calculating the product of two 4-digit numbers, .

By the same process, while accounting for the change in base, we obtain product

We now have

  • 3 double digit multiplications
  • 6 additions and subtractions on 4 digits
  • 6 decimal shifts total

There’s a pattern here!

Suppose we have two numbers and , where both have -digits, is even. By the same process as before, we find that is

Where we have:

  • 3 multiplications on digits
  • 6 additions or subtractions with up to digits
  • decimal shifts total

We can recursively apply this formula until we reach our base case of single digit multiplications!

This is the idea behind Karatsuba’s algorithm - by smartly defining our product, we can minimize the number of single digit multiplications, and thus our time complexity for the overall multiplication!

Karatsuba’s Algorithm

Pseudocode

Given the product of two numbers , where both have digits ( is even), we can minimize the number of single digit multiplications by calculating the product

And recursively applying it on the 3 smaller products

Until we reach our base case of single digit multiplications!

Pseudocode for the algorithm is given below.

def karatsuba(A,B):
    if single_digit(A) || single_digit(B):
       return A * B:
    else:
        digits_half = min(num_digits(A),num_digits(B)) / 2
        A1, A0 = split(A, digits_half)
        B1, B0 = split(B, digits_half)
 
        k1 = karatsuba(A1, B1)
        k2 = karatsuba(A1 + A0, B1 + B0)
        k2 = karatsuba(A0, B0)
 
        return (10^(2 * digits_half)) * k1 + (10^(digits_half)) * (k2 - k3 - k1) + k3

See the below example for Karatsuba’s Algorithm.

Time Complexity

Let’s find the time complexity of Karatsuba’s Algorithm.

If we apply Karatsuba’s algorithm on the product of 2 digit numbers, we perform 3 multipliations on digits, and perform a fixed number of additions and decimal shifts.

This gives us recurrence relation

Which, after applying the master theorem, gives us

Note that the schoolbook method of multiplying 2 n-digit numbers requires single digit multiplications, giving us . Thus, implementing this new algorithm gives us a better time complexity!