Extended Euclid and Modular Inverses

medium
Checkpoint challenge
Solve this to show you know the material and jump straight to Unit 21. Everything before it counts as cleared.

Implement:

  • ext_gcd(a, b): returns (g, x, y) with g = gcd(a, b) and a*x + b*y = g (iteratively)
  • mod_inverse(a, m): the x in 0..m-1 with a*x ≡ 1 (mod m), or raise ValueError if none exists
  • crt(remainders, moduli): the smallest non-negative x satisfying every x ≡ r_i (mod m_i) for pairwise coprime moduli (Chinese remainder theorem)

Don't use pow(a, -1, m).

#number-theory#math2 visible + 1 hidden tests
solution.py · Python 3.13
Loading editor…
Run your code to see results. ⌘↵ to run · ⌘⇧↵ to submit
Extended Euclid and Modular Inverses · TechGradify