W WolfCode · CSC 141

Homework: GCD + Power

作业: 最大公约数 + 幂

≈ 45 min · python-recursion · Open in WolfCode

Euclid's algorithm (300 BCE) + recursive power. Two short recursions in one file.

Euclid's GCD

Python · runnable

Trace gcd(48, 18)

gcd(48, 18) → gcd(18, 12)
gcd(18, 12) → gcd(12,  6)
gcd(12,  6) → gcd( 6,  0)
gcd( 6,  0) → 6                ← base

b shrinks toward 0 each call (because a % b < b), so termination is guaranteed.

Recursive power

Python · runnable

Acceptance criteria

  • All 9 tests pass
  • Both gcd and power are recursive
  • No loops / no ** / no math.gcd / no math.pow