Karatsuba Integer Multiplication

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

NOT EXAMINABLE

A motivating hook for Divide & Conquer, not assessed content β€” stripped from FIT2004 Unit Cheatsheet. The transferable skill that is examinable is reading and off a recurrence and classifying by βž” Solving Recurrences (Telescoping). Read for intuition; do not drill it in SWOTVAC.

Quick Revision

  • 🎯 Objective: multiply two -digit integers sub-quadratically by splitting each in half and doing 3 (not 4) half-size multiplications βž” .
  • πŸ“¦ Core Components: split βž” naive needs 4 products βž” Gauss trick recovers the cross term as .
  • ⚑ Key Constraint: the win is entirely vs β€” the exponent drops from to ; the combine (adds/shifts) stays . Input size is digit count, not value βž” Algorithmic Complexity.

βž— The split and the Gauss trick

With in base :

  • Naive βž” 4 half-size products ⟹ β€” no better than schoolbook.
  • The trick βž” , so 3 products plus additions/shifts. and must be computed first β€” the subtraction is what isolates the cross term.

βš™οΈ Core Implementation

βš–οΈ Core Decision Matrix

MethodRecursive mults RecurrenceRatio Time
Schoolbookβ€”β€”β€”
Naive D&C4 ❌ no gain
Karatsuba3 βœ…
Merge Sort (contrast)2

When It Flips: Karatsuba's constants are larger, so schoolbook wins for small ; libraries switch below a threshold. The claim is about β€” the same cut-over reasoning as merge sort's insertion-sort tail.

πŸ“ˆ Complexity β€” the level-sum written out

Level holds subproblems of size with combine:

  • Leaf-dominated βž” branching (Γ—3) outruns shrinking (Γ·2), so the leaves, not the root combine, are the whole cost. Merge sort’s gives an extra factor instead of an exponent bump.
  • Auxiliary space , not βž” the three calls run sequentially, so one root-to-leaf chain of frames of sizes is live, summing to .

πŸ“Š Manual Execution Trace

⟹ :

StepOperationComputedValue
0 (Init)split at
1recurse β‘ 
2recurse β‘‘
3form sums (carry ⟹ 3 digits)
4recurse β‘’
5Gauss subtract
6shift + add

What it proves: steps 1–2 are reused by step 5, so only one extra product is needed; step 3 realises the carry pitfall live.

🧠 Active Recall