Karatsuba multiplication is a divide-and-conquer algorithm for multiplication of integers. Splitting each operand into two blocks seems to require four smaller products; the Karatsuba three-product identity recovers the two cross terms together from just three products. Repeating that saving through recursion changes the time complexity from quadratic to , where counts digits in a fixed positional notation. The exponent is about : the benefit comes from the algorithm, even though each splitting step needs more addition and subtraction.
The Karatsuba worked multiplication below computes using , , and . The last operand already shows why “half-size” needs care: Karatsuba carry handling and Karatsuba recursion termination account for the extra digit. Karatsuba digit-operation cost states the operation model; Karatsuba practical crossover explains why the asymptotic saving alone does not select a useful threshold.
The original 1962 paper presents a binary numeral system automaton construction: its Theorem 2 and proof reduce multiplication to taking squares of numbers, then reduce a larger square of a number to three smaller squares of numbers. Its footnote explicitly addresses the extra digit in a sum of halves. The sequential time complexity analysis below uses a different operation model from that paper's automaton size and depth bounds, including its Boolean circuit realization without feedback.[ref]
Fix an integer base . Let have at most digits in positional notation, treating zero as one digit. For , set using the ceiling function, and . Euclidean division gives
where and . The floor function selects the high block; the remainder selects the low block. Both operands must use the same , including when their actual digit lengths differ.
For odd , the low block has digit positions and the high block has . Leading zero padding aligns a shorter operand without changing its integer value. Padding to a power-of-two length is optional: the split itself works for every . If power-of-two padding is used, the padded length is less than twice the original length, so it changes only a constant factor in the asymptotic analysis.
Compute exactly three smaller products,
The distributive property gives , so the cross coefficient is . Recombine by
The saving is that and are never separately multiplied. Multiplying a digit block by or shifts its position in positional notation; it does not require another general integer multiplication. Addition of the shifted results still requires Karatsuba carry handling.
Figure 1.
Three products recover the Karatsuba cross coefficient
. The arrows carry exact integer values for the Karatsuba worked multiplication. Only the three coloured product boxes require recursive multiplication; the lower boxes use subtraction, digit shifts and addition. Details.
Use decimal expansion, , , , and . The Karatsuba operand splitting is , . The Karatsuba three-product identity gives
The third product has a three-digit operand. A small-input base case can evaluate these three products directly, or a smaller threshold can continue recursion. Either choice preserves the identity; only the operation count changes.
The same Karatsuba operand splitting handles : choose , , and write , . Then , , , and , giving . A zero high block and unequal lengths require no new algebraic rule.
The blocks in the Karatsuba three-product identity are integers, not single digits: can need positions and can exceed . Concatenating the displayed blocks therefore fails. Accumulate their shifted values with ordinary addition, normalizing each digit position by Euclidean division: a coefficient becomes the digit , with carry passed to the next position. Subtraction likewise propagates borrows. Although for these nonnegative integers, intermediate digitwise subtraction can still borrow.
In the four-digit Karatsuba worked multiplication, grouping by base gives unnormalized coefficients , written from high to low. Normalize from the right: ; ; . The final groups are , yielding . The zero group must retain its two positions.
Since , their sums are less than and can need digits. Allocate that extra digit before the third recursive product; discarding it can change the answer. The GMP manual describes this sum growth and uses a difference-based form to avoid it.[ref]
For the sum-based Karatsuba three-product identity, a safe fixed base-case threshold is three digits or more. Return zero directly if either operand is zero; at or below the threshold, use ordinary digit-by-digit multiplication. Otherwise split by the maximum current operand length and invoke Karatsuba multiplication on , , and .
Each recursive operand has at most digits. This is strictly less than for , proving termination. A rule stopping only at one digit does not supply this guarantee: in base two, splits into , whose sum is , still two digits. An implementation must choose a sufficient threshold or explicitly handle such nonshrinking calls. Leading zero padding must not be repeatedly added in a way that defeats the decreasing size measure.
For signed integers, multiply absolute values with the same nonnegative procedure and restore the sign afterwards. This changes the representation bookkeeping, rather than the three-product identity.
Count a fixed-base digit addition, subtraction, digit copy, or single-digit multiplication as constant cost. Base stays fixed as grows; an entire arbitrary-length integer multiplication is not one constant-cost step. With this model, splitting, sum formation, shifts and recombination require digit operations. Ordinary digit-by-digit multiplication forms digit products for two -position operands. The GMP base-case description uses this rectangular product arrangement.[ref]
The ideal equal-half recurrence relation is
For , level has calls of size . If each internal call incurs exactly times its size in additional digit work, that level contributes . There are leaves. The geometric series of internal work is
Thus fixed positive leaf cost plus this internal work grows on the order of . In Big O notation, . Removing one of four products at every level matters much more than saving just one at the root.
Figure 1.
Karatsuba recursion levels and an explicitly stipulated cost comparison
. Left: the ideal equal-half recursion for , with three children per internal call. Right: model counts for , one unit per leaf and internal overhead for size with . The resulting count is , compared with ordinary digit products. This illustrates the recurrence relation; its constants and crossover are stipulated, rather than measured execution times. Details.
The literal sum-based implementation instead admits the upper bound
for a nondecreasing worst-case cost envelope above a fixed base-case threshold. The extra digit does not change the exponent: since , subtracting three from the size makes each child's adjusted size at most half its parent's. Stop this comparison at any fixed threshold greater than three; its recursion depth is and the level-work sum is bounded by the same geometric series up to constant factors. Exact call counts can depend on carries, zero shortcuts and the threshold, so they need not equal the ideal at each depth.
Karatsuba digit-operation cost explains eventual scaling, while a practical base-case threshold depends on the costs of addition, subtraction, copying and function calls relative to small integer multiplication. Using for a base-case cost, one Karatsuba level costs : the quadratic coefficient falls to , but the linear contribution includes . Small operands can therefore cost more despite the smaller exponent. This comparison and the effect of implementation improvements on the threshold are described in the GMP manual.[ref]
The plotted crossover belongs only to the stipulated constants in its caption; its ordinary-method curve counts digit products alone, while the Karatsuba curve includes the chosen internal overhead. Selecting a threshold for real software requires measuring the actual implementation on its intended machine. With base for a fixed machine digit width , the same analysis counts machine digits; it does not establish a universal crossover in decimal digits, bits, or elapsed time.

Articles by others on the same topic (0)

There are currently no matching articles.