Was suggested to ask this question here.
I'm having a difficult time understanding the Modular Multiplicative Inverse. I'm new to Python and found an example (below) of finding the Mod Inverse and I'd like a better picture (or understanding) of what's happening here to help me further comprehend this.
Why would finding the %(remainders) be useful to find the inverse? After my research, my understanding is finding what I can multiply x by to get (mod m)? (examples below) Why is finding the remainder needed?
I've added a few things below:
(1) I use PythonTutor (screen shot below) which help me get a visual on what exactly is happening step by step. Any further explanation on what's happening in the IF statement would be helpful.
(2) And a example I jotted down on paper to further show how the Inverse is found. I'm including this to show why I don't understand a remainder is needed.
Example:
def modInv(a, m):
for x in range(1, m):
if (((a%m) * (x%m)) % m == 1):
return x
return -1
a = 3
m = 11
print(modInv(a, m))
Output: 4
In PythonTuter, I see the program going through the range to see if the condition is true/false, if true, return x. IF Statement finds the remainder of a%m and multiplies it by the remainder of x%m. Then use that number to find the remainder of m. Why the need to find all these remainders?
Below Image:
So, for this example Inverse of 5 mod 7. I'm able find the inverse pretty quickly in this problem w/o the need for remainders...
Ultimately, I'm really trying to wrap around why remainders are needed in a function above to find the inverse. Any help understanding this would be appreciated.