Closed nilshah98 closed 5 years ago
[Function: M.modInverse(a, b)]
Validation:
gcd(a, b) = 1
else return an error indicating that a & b aren't relatively prime!Category: Mathematical Utilities
@pbiswas101 For this we need to implement a power function with modulo M in O(log n) time. So, we have to build it as another function of MathBall. Is it okay to make an issue on Power function? .
@Indrajit1997 Alright, you can raise an issue discussing its implementation as M.pow(a, b, m)
with the last parameter being optional! So, that it can also be used as M.pow(a, b)
@pbiswas101 For this we need to implement a power function with modulo M in O(log n) time. So, we have to build it as another function of MathBall. Is it okay to make an issue on Power function?
The modular multiplicative inverse can be implemented using Extended Euclid Theorem, which doesn't require any power function with modulo M.
Additionally, here are a few points for implementing modulo multiplicative inverse -
Example Output -
M.modInv(10,460) = 0; /* 10,460 not relatively prime, so return 0 */
M.modInv(3,26) = 9; /* 3,26 relatively prime, so return modulo multiplicative inverse */
Complexity for Extended Euclid as well as Fermat is both - O(Log m)
Validation -
PS: Add more validations, if any
Can I work on this ?
@devRD You're assigned!
Do the checklist before filing the issue:
NOTE: Provide a clear and concise description of the feature that needs to be added! Or if its a bug, then provide the necessary steps to reproduce it along with screenshots.
In many competitive coding questions it is required to get modulo multiplicative inverse of your answer with some other number, many a times, it's prime - 10^9+7. So, an added functionality to find modulo multiplicative inverse of two numbers.
Maybe a separate function, for when the numbers are prime, coprime, and have factors.
Links- Modular Multiplicative Inverse GFG