Implement `xgcd(a, b)` from scratch, returning a triple `(g, s, t)` with $a \cdot s + b \cdot t = g = \gcd(a, b)$. Verify Bézout's identity holds on at least 30 input pairs covering positive, negative, zero, coprime, and large random cases. Then use your implementation to solve the linear Diophantine equation $847 x + 609 y = \gcd(847, 609)$, finding both a particular solution and the general family of solutions. The deliverable is a working library plus a write-up that convinces a sceptical reader your implementation is correct on every edge case.
A test run that ends with something like:
xgcd(0, 0) -> g=0, s=0, t=0
xgcd(0, 5) -> g=5, s=0, t=1 verify: 0*0 + 5*1 = 5 OK
xgcd(5, 0) -> g=5, s=1, t=0 verify: 5*1 + 0*0 = 5 OK
xgcd(-12, 18) -> g=6, s=-1, t=-1 verify: -12*-1 + 18*-1 = 12 - 18 = -6 ... fix sign convention
xgcd(1071, 462) -> g=21, s=-3, t=7 verify: 1071*-3 + 462*7 = 21 OK
xgcd(847, 609) -> g=7, s=...,t=... verify: 847*s + 609*t = 7 OK
... 24 more ...
All 30 tests passed.
…followed by a Markdown section reading roughly:
gcd(847, 609) = 7.
Bézout: 847 * (-5) + 609 * 7 = -4235 + 4263 = 28 ... (work out actual coefficients)
Particular solution to 847x + 609y = 7: (x0, y0) = (s, t) from xgcd.
General solution: (x, y) = (x0 + k * 609/7, y0 - k * 847/7) = (x0 + 87k, y0 - 121k), k in Z.
xgcd_rec(a, b) that returns the same triple, and verify it agrees with the iterative version on all 30 test pairs. The recursion is: if return ; else recurse on and reconstruct.xgcd to compute the modular inverse of 17 modulo (a prime commonly used in competitive programming and hash-table seeding). Verify .xgcd against math.gcd on pairs of 1024-bit random integers. The Python overhead of your loop will make it slower, but the number of iterations should match Lamé's bound — count them and plot iteration count vs for 100 random pairs.xgcd output, and write down the general solution.