Karatsuba Algorithm

Introduction to Karatsuba Algorithm

  • The Karatsuba algorithm, a foundational method for performing efficient multiplication of large numbers in computer science1. Taught in Week 1 of the Dynamic Programming and Greedy Algorithms course at CU Boulder, the algorithm stands as a testament to the power of a divide-and-conquer strategy.

  • The Karatsuba algorithm, named after Anatolii Alexeevitch Karatsuba, strategically breaks down the task of multiplying two n-digit numbers into simpler subtasks, avoiding the traditional computational complexity of multiplication. This approach illustrates the multiplicative properties of numbers and the time-saving potentials of recursion and divide-and-conquer techniques in algorithm development.

  • In the following sections, we will delve into the mechanics of the Karatsuba algorithm, discuss its complexity, and examine applications where this algorithm truly shines.

The Mechanics of Karatsuba Algorithm

The Karatsuba algorithm commences multiplication by splitting both numbers into two halves.

The upper half (most significant digits) is labeled as ‘a’ and ‘c’, and the lower half (least significant digits) is correspondingly ‘b’ and ‘d’.

The algorithm breaks the traditional multiplication into three main multiplications, namely, ac, bd, and (a+b)(c+d).

Calculation of (a+b)(c+d) results in obtaining the sum of ac, bd, and a pair of cross products (ad+bc).

To receive the final result, ac is shifted left by m digits (where m is the number of digits in smaller half), and bd stays unaltered.

This makes the algorithm faster than classical multiplication for sufficiently large numbers.

Karatsuba Multiplication Example

Here is an example that demonstrates how to use Karatsuba’s algorithm to calculate the multiplication of larger numbers. Let’s take the example of multiplying two 4-digit numbers: let’s say we want to calculate 1234 * 5678.

Input:

Let’s consider two numbers:

  • a = 1234
  • b = 5678

Step 1: Splitting the Numbers

  1. Split each number into two halves:
    • a = 12, 34
    • b = 56, 78

Step 2: Calculate Three Products

  1. Calculate ac: 12 * 56 = 672
  2. Calculate bd: 34 * 78 = 2652
  3. Calculate (a+b)(c+d): (12+34)(56+78) = 46 * 134 = 6164

Step 3: Calculate Cross Products

  1. Subtract ac and bd from (a+b)(c+d):
    • 6164 - 672 = 5492
    • 5492 - 2652 = 2840

Step 4: Combine Results

  1. Shift ac left by the number of digits in smaller half:
    • Shift 672 left by 2 digits: 67200
  2. Add the results:
    • ac * 10^4 + (ad+bc) * 10^2 + bd = 67200 + 2840 * 100 + 2652 = 7006652

Output:

The product of 1234 and 5678 is 7006652.

Code Resources

Wrapping Up the Discussion

In this course, we delved into the operations of addition and multiplication for large numbers. Large numbers refer to numbers longer than the computer’s word length, and they find wide applications in various real-world scenarios, particularly in fields like cryptography.

  • Addition Algorithm

    • The addition algorithm involves adding digits starting from the least significant bit and considering carryovers. If there’s a carryover, it gets added to the next digit’s calculation. Eventually, we obtain the sum of two large numbers.
  • Multiplication Algorithm

    • For multiplication, we employ a method known as “vertical multiplication.” Each digit of one large number is multiplied with every digit of the other, and the results are added up. Carryovers are handled similarly to addition. Ultimately, we arrive at the product of two large numbers.
  • Karatsuba Algorithm

    • In addition to the traditional multiplication algorithm, we also introduced the Karatsuba algorithm. The Karatsuba algorithm utilizes the divide-and-conquer approach, breaking down the multiplication problem of large numbers into smaller subproblems. By recursively solving these subproblems, the final result is obtained. This algorithm can significantly reduce the number of multiplication operations in certain cases, thus enhancing efficiency.

These algorithms not only effectively handle operations with large numbers but are also frequently required in fields like cryptography. Mastering these algorithms not only deepens our understanding of numerical operations but also allows us to apply this knowledge to solve practical engineering and scientific problems.

Reference

1 Karatsuba, Anatolii Alexeevitch. “The Complexity of Multiplication in Generic Algebras.” Proceedings of Steklov Institute of Mathematics 211 (1995): 169-183.