# 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?

2. Because you can't

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

Mod has no inverse.

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.

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

5. Now , I understand

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

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?

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.....

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!

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

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

=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))

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)

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.

There are currently 1 users browsing this thread. (0 members and 1 guests)

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1