Here you can find the source of modInverse(int a, int n)
public static int modInverse(int a, int n)
//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; } }