Algorithms

Karatsuba multiplication

Can we multiply two numbers and save one multiplication? Split each number into two parts and see how Karatsuba gets the same answer using three products instead of four.

In the 1234 × 5678 example, select “Break down” on a block product to see its steps.

23 = 2 × 10 + 342 = 4 × 10 + 2

Split each number into a high and a low part.

Ordinary block multiplication

Block multiplications
0
Additions and subtractions
0

Karatsuba multiplication

Block multiplications
0
Additions and subtractions
0

Place-value shifts are excluded from the multiplication count.

Video walkthrough

Where the four products come from

Take 23 × 42. The first number has 2 tens and 3 ones, while the second has 4 tens and 2 ones. Multiplying each part of the first number by each part of the second gives four products.

The high parts give 2 × 4 = 8 hundreds, and the low parts give 3 × 2 = 6 ones. The two cross products give 2 × 2 = 4 tens and 3 × 4 = 12 tens. Put them together to get 800 + 160 + 6 = 966. The 16 tens contribute 160 when we add the terms.

How Karatsuba gets by with three

Only the sum of the two cross products is needed to assemble the answer. Karatsuba starts with the same high and low products, 8 and 6. Then it adds the parts of each number and multiplies the sums: (2 + 3) × (4 + 2) = 30.

That 30 already contains all four products from the first method. Subtract the two we know: 30 − 8 − 6 = 16. This gives both cross products together. We can now assemble the same 800 + 160 + 6.

(a + b)(c + d) − ac − bd = ad + bc

What the counters measure

The interactive shows one split at each level. If the low part takes k digits, B = 10ᵏ, and we write the numbers as aB + b and cB + d. Reassembly follows the same rule every time: shift ac by 2k places, shift the sum ad + bc by k places and leave bd in place. Treat missing high digits in the shorter operand as zeros.

The multiplication counter counts products of blocks, including the product of the sums in Karatsuba. Place-value shifts are excluded. Additions and subtractions, including reassembly, are counted separately. In this split, the ordinary method uses four multiplications and three additions. Karatsuba uses three multiplications and six additions or subtractions.

(aB + b)(cB + d) = ac · B² + (ad + bc) · B + bd

Why repeat the split?

When the parts themselves are long, each of the three products can be computed the same way. For inputs of similar length, ordinary multiplication takes O(n²) digit operations while Karatsuba takes about O(n^1.585), assuming addition and splitting take O(n). Here n is the number of digits, and 1.585 is the rounded base-two logarithm of three.

Choose 1234 × 5678 and advance to the product 12 × 56. Select “Break down” to explore it as a new problem with the same steps. You can return to the same step on the parent level. The counters apply to the current level. A nested product can be opened as long as both operands have more than one digit.

On short inputs, the extra additions, subtractions and calls can outweigh the saving. In practice, ordinary multiplication is used below a size threshold.

JavaScript implementation

This example accepts nonnegative BigInt values. If either operand is a single digit, it uses ordinary multiplication. Otherwise, each recursive step computes the high product, the low product and the product of the sums.

function karatsuba(x, y) {
    if (x < 0n || y < 0n) throw new RangeError("Expected nonnegative integers");
    if (x < 10n || y < 10n) return x * y;

    const digits = Math.max(x.toString().length, y.toString().length);
    const base = 10n ** BigInt(Math.floor(digits / 2));
    const a = x / base, b = x % base;
    const c = y / base, d = y % base;

    const high = karatsuba(a, c);
    const low = karatsuba(b, d);
    const mixed = karatsuba(a + b, c + d) - high - low;
    return (high * base + mixed) * base + low;
}

console.log(karatsuba(23n, 42n)); // 966n

Karatsuba multiplication in the GNU MP manual