Java Integer Mod modInverse(int a, int n)

Here you can find the source of modInverse(int a, int n)

Description

calculates and returns the inverse of a modulo n, both of which should be positive.

License

Open Source License

Declaration

public static int modInverse(int a, int n) 

Method Source Code

//package com.java2s;
//License from project: Open Source License 

public class Main {
    /**//from  www  .j av a  2  s .c  o m
     * calculates and returns the inverse of a modulo n, both of which should be positive.  If the
     * inverse does not exist, 0 is returned.
     */
    public static int modInverse(int a, int n) {
        int i = n, v = 0, d = 1;
        while (a > 0) {
            int t = i / a, x = a;
            a = i % x;
            i = x;
            x = d;
            d = v - t * x;
            v = x;
        }
        v %= n;
        if (v < 0)
            v = (v + n) % n;
        return v;
    }
}

Related

  1. modifyAlpha(int color, int alpha)
  2. modifyBase32AtIndex(final String s, final int index)
  3. modifyDummyString(String dummyString, int beginTag, int endTag)
  4. modifyPublicPortCheckRange(String modifiedVal, int dashCnt)
  5. modifyString(char firstCharacter, String srcString, int indexOfSubstring)
  6. modInverse(int n, int mod)
  7. modMultiply(long a, long b, int m)
  8. modPos(int divisor, int dividend)
  9. modSubtract(long a, long b, int m)