+ Reply to Thread
Results 1 to 12 of 12

How do I find the inverse of a MOD function

  1. #1
    Registered User
    Join Date
    01-01-2008
    Posts
    16

    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. #2
    Forum Expert mikerickson's Avatar
    Join Date
    03-30-2007
    Location
    Davis CA
    MS-Off Ver
    Excel 2011
    Posts
    6,229
    Because you can't

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


    Mod has no inverse.
    _
    ...How to Cross-post politely...
    ..Wrap code by selecting the code and clicking the # or read this. Thank you.

  3. #3
    Registered User
    Join Date
    01-01-2008
    Posts
    16

    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. #4
    Forum Expert shg's Avatar
    Join Date
    06-20-2007
    Location
    The Great State of Texas
    MS-Off Ver
    2003, 2010
    Posts
    40,678
    Try this:
    Please Login or Register  to view this content.
    Last edited by shg; 01-02-2008 at 11:26 AM.

  5. #5
    Forum Expert mikerickson's Avatar
    Join Date
    03-30-2007
    Location
    Davis CA
    MS-Off Ver
    Excel 2011
    Posts
    6,229
    Now , I understand

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

  6. #6
    Registered User
    Join Date
    01-01-2008
    Posts
    16

    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. #7
    Forum Expert daddylonglegs's Avatar
    Join Date
    01-14-2006
    Location
    England
    MS-Off Ver
    2016
    Posts
    14,675
    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.....
    Last edited by daddylonglegs; 01-01-2008 at 09:46 PM.

  8. #8
    Registered User
    Join Date
    01-01-2008
    Posts
    16

    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. #9
    Forum Expert shg's Avatar
    Join Date
    06-20-2007
    Location
    The Great State of Texas
    MS-Off Ver
    2003, 2010
    Posts
    40,678
    =MATCH(1,INDEX(MOD(ROW(INDIRECT("1:"&A1))*B1,A1),0 ),0)
    Very nice, dll.

  10. #10
    Registered User
    Join Date
    10-02-2022
    Location
    Brisbane, Australia
    MS-Off Ver
    Excel 2019
    Posts
    2

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

    Quote Originally Posted by daddylonglegs View Post
    =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. #11
    Forum Moderator Glenn Kennedy's Avatar
    Join Date
    07-08-2012
    Location
    Digital Nomad... occasionally based in Ireland.
    MS-Off Ver
    O365 (PC) V 2403
    Posts
    44,002

    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)
    Last edited by Glenn Kennedy; 10-02-2022 at 06:31 AM.
    Glenn




    None of us get paid for helping you... we do this for fun. So DON'T FORGET to say "Thank You" to all who have freely given some of their time to help YOU.

    Temporary addition of accented to illustrate ongoing problem to the TT: L? fh?ile P?draig sona dhaoibh

  12. #12
    Registered User
    Join Date
    10-02-2022
    Location
    Brisbane, Australia
    MS-Off Ver
    Excel 2019
    Posts
    2

    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.
    Last edited by Dave Keenan; 10-02-2022 at 09:01 AM.

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

Bookmarks

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