Karatsuba Integer Multiplication

Context: FIT2004_MOC · a Week 1 Divide and Conquer algorithm · beats schoolbook by trading one multiplication for cheap additions · analysed by solving its recurrence

NOT EXAMINABLE

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 and off a recurrence and classifying by — see Solving Recurrences (Telescoping). Read this note for intuition only; do not spend SWOTVAC time drilling it.

Quick Revision

  • 🎯 Objective: multiply two -digit integers in sub-quadratic time by splitting each in half and doing only 3 (not 4) half-size multiplications ➔ .
  • 📦 Core Components: split ➔ naive needs (4 mults) ➔ Gauss trick recovers the cross term as (1 extra mult, reusing 2).
  • ⚡ Key Constraint: the win is entirely in vs in the recurrence — it drops the exponent from to ; the combine work (adds/shifts) stays .

📝 Why is the input size that matters

  • Input size = number of digits/bits, not the numeric value ➔ multiplying two -digit numbers, the cost is a function of (see Algorithmic Complexity, where “input size is often bit-length”).
  • Schoolbook (long) multiplication ➔ every digit of times every digit of digit-multiplications.

➗ The divide-and-conquer split

Write both numbers in base (e.g. ) with a half-split :

  • Naive ➔ compute and both cross products ⟹ 4 half-size multiplications ⟹ — no better than schoolbook.

⭐ The Karatsuba (Gauss) trick — 4 → 3

Compute the middle term without a third and fourth multiplication: The identity, expanded — the one line that justifies the trick if asked to prove it:

  • We already have and ; the single product gives the rest by subtraction3 multiplications a few additions/shifts.

⚙️ Core Implementation

⚖️ Core Decision Matrix

MethodRecursive mults RecurrenceLevel-sum ratio TimeBeats schoolbook?
Schoolbookbaseline
Naive D&C4❌ (same)
Karatsuba3
Merge Sort (contrast)2

When It Flips: Karatsuba's constant factors are larger, so for small schoolbook is faster; real implementations switch to schoolbook below a threshold. Karatsuba wins asymptotically, as .

📈 Complexity

MeasureBestAverageWorstNote
Time; no input-dependent branching, so all cases equal
Space (auxiliary)one root-to-leaf path of live frames — derived below
Recursion depthhalving to a 1-digit base
Recursive calls / level at level leaves

Time — the level-sum written out

Level holds subproblems of size , each doing combine work: Ratio geometric, dominated by its last term (the leaves), so

  • Contrast ➔ merge sort’s gives , every level costs equally ⟹ the extra factor instead of an exponent bump. Full machinery in Solving Recurrences (Telescoping).

Space — why , not

  • 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 .
  • Halving sum converges — counting frames of size each (⟹ ) is the standard over-estimate.

⚠️ Common Mistakes

  • 💡 Shifts are not multiplications ➔ multiplying by appends zeros ( work); only the three recursive calls count toward .
  • 💡 The trick needs 2 reused products alone is useless; the subtraction is what isolates the cross term, so and must be computed first.
  • 💡 Asymptotic, not universal, speedup ➔ larger hidden constants mean schoolbook wins for small — quote as an claim.
  • 💡 can carry an extra digit ➔ the sums may be digits; a correct implementation handles the carry (the recurrence bound is unaffected).
  • 💡 , not ➔ the high term is shifted by digits; an off-by-one in the shift silently corrupts the product while the complexity argument still “looks” right.

📊 Exam Execution Trace

Manual Execution Trace

, , , , :

Step / StateOperationComputedValue
0 (Init)split at
1recurse ①
2recurse ②
3form sums (carry ⟹ 3 digits)
4recurse ③
5Gauss subtract
6shift + add

Final Extracted Output: using 3 multiplications of 2-digit operands, not 4.

  • What the trace proves ➔ step 3 realises the carry pitfall live ( needs digits); steps 1–2 are reused by step 5, which is why only one extra product is needed.

🧠 Active Recall