A small kernel of code for playing with Galois fields of arbitrary characteristic
修订版 | 698f6818f630ab65f89ba3649a0e205b4f3432f3 (tree) |
---|---|
时间 | 2020-02-20 13:27:56 |
作者 | Eric Hopper <hopper@omni...> |
Commiter | Eric Hopper |
No more fumbling with the multiplicative inverse in presentations.
@@ -54,3 +54,15 @@ | ||
54 | 54 | result = op(a, b) |
55 | 55 | print(f' {result:{width}} |', end='') |
56 | 56 | print() |
57 | + | |
58 | + | |
59 | +def mult_inverses(a, b): | |
60 | + am, bm, g = extended_gcd(a, b) | |
61 | + if g == 0: | |
62 | + raise ValueError(f"{a} and {b} are not relatively prime.") | |
63 | + if am < 0: | |
64 | + am = b + am | |
65 | + if bm < 0: | |
66 | + bm = a + bm | |
67 | + return f"{a} * {am} % {b} == {a * am % b}"\ | |
68 | + f"\n{b} * {bm} % {a} == {b * bm % a}" |