[issue36027] Consider adding modular multiplicative inverse to the math module

Tim Peters report at bugs.python.org
Tue Feb 19 00:34:15 EST 2019


Tim Peters <tim at python.org> added the comment:

- Some form of this would be most welcome!

- If it's spelled this way, put the modulus argument last?  "Everyone expects" the modulus to come last, whether in code:

    x = (a+b) % m
    x = a*b % m
    x = pow(a, b, m)

or in math:
    
    a^(k*(p-1)) = (a^(p-1))^k = 1^k = 1 (mod p)

- Years ago Guido signed off on spelling this

    pow(value, -1, modulus)

which currently raises an exception.  Presumably

    pow(value, -n, modulus)
    
for int n > 1 would mean the same as pow(pow(value, -1, modulus), n, modulus), if it were accepted at all.  I'd be happy to stop with -1.

- An alternative could be to supply egcd(a, b) returning (g, x, y) such that

     a*x + b*y == g == gcd(a, b)

But I'm not sure anyone would use that _except_ to compute modular inverse.  So probably not.

----------

_______________________________________
Python tracker <report at bugs.python.org>
<https://bugs.python.org/issue36027>
_______________________________________


More information about the Python-bugs-list mailing list