+ Reply to Thread
Results 1 to 12 of 12

How do I find the inverse of a MOD function

Hybrid View

  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
    2010, 2019
    Posts
    40,689
    Try this:
    Function ModInv(b As Long, m As Long) As Variant
        ' Returns the modular inverse of b Mod m using the
        ' Extended Euclidean Algorithm to compute integers x and y
        ' {x, y || b * x + m * y = GCD(b, m) == 1 (else the inverse does not exist)}
        ' i.e., Mod(x * b, m) = 1
        ' Algorithm from Junaid Majeed at
        ' http://www.codeproject.com/KB/recipes/eealgo.aspx
    
        Dim ak As Long, ak1 As Long, ak2 As Long
        Dim xk As Long, xk1 As Long, xk2 As Long
        Dim yk As Long, yk1 As Long, yk2 As Long
        Dim qk As Long, qk1 As Long, qk2 As Long
    
        If Abs(GCD(b, m)) <> 1 Then
            ModInv = CVErr(xlErrValue)
            Exit Function
        Else
            ak1 = b
            xk1 = 1
            yk1 = 0
    
            ak = m
            xk = 0
            yk = 1
            qk = Int(ak1 / ak)
    
            Do
                ak2 = ak1: ak1 = ak
                xk2 = xk1: xk1 = xk
                yk2 = yk1: yk1 = yk
                qk2 = qk1: qk1 = qk
                
                ak = ak2 - qk1 * ak1
                If ak = 0 Then Exit Do
                
                xk = xk2 - qk1 * xk1
                yk = yk2 - qk1 * yk1
                qk = Int(ak1 / ak)
            Loop
        End If
        ModInv = xk1
    End Function
    
    Function GCD(ByVal i1 As Long, ByVal i2 As Long) As Long
        If i2 = 0 Then
            GCD = i1
        Else
            GCD = GCD(i2, i1 Mod i2)
        End If
    End Function
    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 Moderator Glenn Kennedy's Avatar
    Join Date
    07-08-2012
    Location
    Digital Nomad... occasionally based in Ireland.
    MS-Off Ver
    O365 (PC) V 2406
    Posts
    44,662

    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

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