# How do I find the inverse of a MOD function

1. ## How do I find the inverse of a MOD function

I'm trying to find out how to do the inverse of a MOD function.

7mod(23) = 7 That's easy enough in excel to do =MOD(7,23)

However the inverse of 7mod(23) = 10 I haven't found a way to compute the inverse of a mod function with excel.

Does anyone out there know how to do it?  Register To Reply

2. Because you can't

If Mod(x,7)=3, x could be 3,10,17,....

Mod has no inverse.  Register To Reply

3. ## MOD has inverses

You are right that MOD(x,7) x has multiple values however you are wrong that mod has no inverse.

Maybe I need to be more explicit. Some values for mod have no inverses but for the ones that do I want excel to calculate them.

MOD(7,23) has 10 as it's inverse.

As an example I will calculate a column of values for MOD(x,23) x=1..10 and I will get a consecutive set of numbers 1 to 10. Now the next column will show the inverses for MOD(x,23) where x has a value from one to ten. I SHOULD get 1, 12, 8, 6, 14, 4, 10, 3, 18, and 7 as each of their inverses. How do I get excel to calculate these for me.

Mathematics programs can easily calculate these values.  Register To Reply

4. Try this: ``Please Login or Register  to view this content.``  Register To Reply

5. Now , I understand

The multiplicitive inverse. Given A and N, find B such that Mod(A*B,N)=1.  Register To Reply

6. ## Yes

Yes! That's exactly it, the multiplicative inverse. Sorry I was a little vague in the beginning.

And thanks for the code!

I guess there isn't any other way to do it without using some sort of code programming is there?  Register To Reply

7. Here's a formula approach.....

If you want the inverse of MOD(7,23) then assuming A1 contains 23 and B1 7 then use this formula in C1 to get the inverse

=MATCH(1,INDEX(MOD(ROW(INDIRECT("1:"&A1))*B1,A1),0),0)

gives #N/A if there is no inverse

Note: this only gives a positive number whereas shg's code may give a negative value.....  Register To Reply

8. ## Awesome! Thanks!

Great! That's exactly what I was looking for and I thank everyone elses input as well. Greatly appreciated.

Happy New year everyone!  Register To Reply

9. =MATCH(1,INDEX(MOD(ROW(INDIRECT("1:"&A1))*B1,A1),0 ),0)
Very nice, dll.  Register To Reply

10. ## Re: How do I find the inverse of a MOD function Originally Posted by daddylonglegs =MATCH(1,INDEX(MOD(ROW(INDIRECT("1:"&A1))*B1,A1),0),0)
I realise I'm 14 years late to the party, but that is the most mind-blowing Excel formula I've ever seen. Daddylonglegs modestly disclaims its completeness. But it can easily be made into a complete Modular Inverse as follows, where A1 is the modulus, and B1 is the other argument:

=IF(ABS(A1)=1,0,SIGN(A1)*MATCH(1,INDEX(MOD(ROW(INDIRECT("1:"&ABS(A1)))*SIGN(A1)*B1,ABS(A1)),0),0))  Register To Reply

11. ## Re: How do I find the inverse of a MOD function

I, too, know it's old... but is it correct???

https://www.geeksforgeeks.org/multip...nder-modulo-m/

In which case, it should be:

=MATCH(1,INDEX(MOD(\$A\$1*ROW(INDIRECT("1:"&\$B\$1)),\$B\$1),0),0)

or (O365)

=MATCH(1,MOD(\$A\$1*SEQUENCE(\$B\$1),\$B\$1),0)  Register To Reply

12. ## Re: How do I find the inverse of a MOD function

Hi Glen. Daddylonglegs made it clear that for his formula, the modulus was in A1 and the other argument was in B1. That gives correct results for moduli greater than 1. And with my wrapper, it gives correct results modulo 1 and for negative moduli as well, as checked against ModularInverse[] in Wolfram Language.

You seem to have merely swapped the arguments, but you haven't said which one is the modulus.  Register To Reply