Karatsuba is a motivating hook for Divide & Conquer, not assessed content. It has been stripped from FIT2004 Unit Cheatsheet and the exam-oriented notes; the transferable skill that is examinable is reading a and b off a recurrence and classifying by r=a/b — see Solving Recurrences (Telescoping). Read this note for intuition only; do not spend SWOTVAC time drilling it.
Quick Revision
🎯 Objective: multiply two n-digit integers in sub-quadratic time by splitting each in half and doing only 3 (not 4) half-size multiplications ➔ Θ(nlog23)≈Θ(n1.585).
📦 Core Components: split x=xLBm+xR ➔ naive needs xLyL,xRyR,xLyR,xRyL (4 mults) ➔ Gauss trick recovers the cross term as (xL+xR)(yL+yR)−xLyL−xRyR (1 extra mult, reusing 2).
⚡ Key Constraint: the win is entirely in a=3 vs a=4 in the recurrence — it drops the exponent from log24=2 to log23≈1.585; the combine work (adds/shifts) stays Θ(n).
📝 Why n is the input size that matters
Input size = number of digits/bits, not the numeric value ➔ multiplying two n-digit numbers, the cost is a function of n (see Algorithmic Complexity, where “input size is often bit-length”).
Schoolbook (long) multiplication ➔ every digit of x times every digit of y ⟹ Θ(n2) digit-multiplications.
➗ The divide-and-conquer split
Write both numbers in base B (e.g. B=10) with a half-split m=n/2:
x=xLBm+xR,y=yLBm+yRx⋅y=AxLyLB2m+middle(xLyR+xRyL)Bm+CxRyR
Naive ➔ compute A,C and both cross products ⟹ 4 half-size multiplications ⟹ T(n)=4T(n/2)+Θ(n)=Θ(n2) — no better than schoolbook.
⭐ The Karatsuba (Gauss) trick — 4 → 3
Compute the middle term without a third and fourth multiplication:
middle=xLyR+xRyL=(xL+xR)(yL+yR)−A−CThe identity, expanded — the one line that justifies the trick if asked to prove it:
We already have A=xLyL and C=xRyR; the single product (xL+xR)(yL+yR) gives the rest by subtraction ⟹ 3 multiplications + a few Θ(n) additions/shifts.
⚙️ Core Implementation
Raw recursive Karatsuba (no built-in big-int multiply for the recursion)
def karatsuba(x, y, n): # x, y are n-digit non-negative integers (n a power of 2) if n == 1: # base case: one-digit multiply return x * y m = n // 2 Bm = 10 ** m # base^(n/2) — a shift, not a multiply xl, xr = x // Bm, x % Bm # high / low halves yl, yr = y // Bm, y % Bm A = karatsuba(xl, yl, m) # 1st recursive mult C = karatsuba(xr, yr, m) # 2nd recursive mult mid = karatsuba(xl + xr, yl + yr, m) # 3rd recursive mult mid = mid - A - C # Gauss trick: recover cross term return A * (Bm * Bm) + mid * Bm + C # shifts + adds are Θ(n)
💡 Common Mistake: the ×10m / ×102m are digit shifts (Θ(n)), not counted as multiplications — only the three karatsuba(...) calls drive the recurrence. Miscounting them as multiplications re-derives O(n2).
When It Flips: Karatsuba's constant factors are larger, so for small n schoolbook is faster; real implementations switch to schoolbook below a threshold. Karatsuba wins asymptotically, as n→∞.
📈 Complexity
Measure
Best
Average
Worst
Note
Time
Θ(nlog23)
Θ(nlog23)
Θ(nlog23)
≈n1.585; no input-dependent branching, so all cases equal
Space (auxiliary)
Θ(n)
Θ(n)
Θ(n)
one root-to-leaf path of live frames — derived below
Recursion depth
log2n
log2n
log2n
halving to a 1-digit base
Recursive calls / level
3i at level i
—
—
3log2n=nlog23 leaves
Time — the level-sum written out
Level i holds 3i subproblems of size n/2i, each doing Θ(size) combine work:
T(n)=∑i=0log2n3i⋅c2in=cn∑i=0log2n(23)i
Ratio r=23>1 ⟹ geometric, dominated by its last term (the leaves), so
T(n)=Θ(n(23)log2n)=Θ(n⋅2log2n3log2n)=Θ(n⋅nnlog23)=Θ(nlog23)
Contrast ➔ merge sort’s a=2,b=2 gives r=1, every level costs cn equally ⟹ the extra logn factor instead of an exponent bump. Full machinery in Solving Recurrences (Telescoping).
Space — why Θ(n), not Θ(nlogn)
Only one path is live at a time ➔ the three calls run sequentially, so the stack holds one root-to-leaf chain: frames of size n,n/2,n/4,…,1.
Halving sum converges ➔ ∑i=0log2ncn/2i<2cn=Θ(n) — counting log2n frames of size n each (⟹ Θ(nlogn)) is the standard over-estimate.
⚠️ Common Mistakes
💡 Shifts are not multiplications ➔ multiplying by Bm appends zeros (Θ(n) work); only the three recursive calls count toward a.
💡 The trick needs 2 reused products ➔ (xL+xR)(yL+yR) alone is useless; the subtraction −A−C is what isolates the cross term, so A and C must be computed first.
💡 Asymptotic, not universal, speedup ➔ larger hidden constants mean schoolbook wins for small n — quote Θ(n1.585) as an n→∞ claim.
💡 (xL+xR) can carry an extra digit ➔ the sums may be m+1 digits; a correct implementation handles the carry (the recurrence bound is unaffected).
💡 B2m, not Bm2 ➔ the high term is shifted by 2m digits; an off-by-one in the shift silently corrupts the product while the complexity argument still “looks” right.
Final Extracted Output:1234×5678=7,006,652 using 3 multiplications of 2-digit operands, not 4.
What the trace proves ➔ step 3 realises the carry pitfall live (yL+yR=134 needs m+1=3 digits); steps 1–2 are reused by step 5, which is why only one extra product is needed.
🧠 Active Recall
Karatsuba and naive divide-and-conquer both split the numbers in half — why is only one sub-quadratic?
Answer
Short answer: naive D&C computes four half-size products, giving T(n)=4T(n/2)+Θ(n)=Θ(n2) — no gain. Karatsuba computes only three (recovering the cross term by (xL+xR)(yL+yR)−A−C), giving T(n)=3T(n/2)+Θ(n)=Θ(nlog23).
Why:The exponent is log2a ➔ telescoping the recurrence gives a geometric level-sum with ratio a/2 dominated by the alog2n=nlog2a leaves (combine work is only Θ(n)); dropping a from 4 to 3 moves the exponent from 2 to ≈1.585 — the entire speedup.
In the Karatsuba recurrence, why does multiplying by 10m not count as one of the recursive multiplications?
Answer
Short answer: multiplying an integer by 10m (base power) just appends m zero digits — a linear-time shift, part of the Θ(n) combine cost, not a general multiplication of two n-digit numbers.
Why:Only same-size products recurse ➔ the recurrence counts sub-multiplications of two ∼n/2-digit operands (a=3 of them); shifts and additions are the f(n)=Θ(n) term, so treating a shift as a fourth “multiply” would wrongly inflate a back to 4 and re-derive Θ(n2).
Derive Θ(nlog23) from T(n)=3T(n/2)+cn by summing the recursion tree, without quoting the Master Theorem.
Answer
Short answer: level i costs 3i⋅cn/2i=cn(3/2)i; summing i=0…log2n gives cn∑(3/2)i, a geometric series with r=23>1, so the last term dominates: Θ(n(3/2)log2n)=Θ(nlog23).
Why:Branching outruns shrinking ➔ each level triples the subproblem count while only halving their size, so work grows downward and the nlog23 leaves — not the root combine — are the entire cost.
Karatsuba is asymptotically faster than schoolbook, yet production big-integer libraries still call schoolbook. Reconcile this.
Answer
Short answer:Θ hides constants. Karatsuba pays three recursive calls plus several Θ(n) additions, subtractions and shifts per level; below a threshold n0 (typically tens of digits) schoolbook’s tiny constant wins, so libraries recurse only until n≤n0 then switch.
Why:Asymptotic ≠ always ➔ Θ(n1.585) is a claim about n→∞; the crossover is where cKn1.585=cSn2, i.e. n0=(cK/cS)1/0.415 — a large constant ratio pushes n0 high. Same reasoning as merge/quick sort cutting over to insertion sort on small slices.