Karatsuba recursion termination
ID: karatsuba-recursion-termination
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.
New to topics? Read the docs here!