result = euclidInv(y, rem, quot)[swapped ? 0 : 1];
BigInteger[] xy = new BigInteger[2]; euclidInv(y, rem, quot, xy); result = swapped ? xy[0] : xy[1];
  public BigInteger modInverse(BigInteger y)  {    if (y.isNegative() || y.isZero())      throw new ArithmeticException("non-positive modulo");    // Degenerate cases.    if (y.isOne())      return ZERO;    else if (isOne())      return ONE;    // Use Euclid's algorithm as in gcd() but do this recursively    // rather than in a loop so we can use the intermediate results as we    // unwind from the recursion.    // Used http://www.math.nmsu.edu/~crypto/EuclideanAlgo.html as reference.    BigInteger result = new BigInteger();    int xval = ival;    int yval = y.ival;    boolean swapped = false;    if (y.words == null)      {	// The result is guaranteed to be less than the modulus, y (which is	// an int), so simplify this by working with the int result of this	// modulo y.  Also, if this is negative, make it positive via modulo	// math.  Note that BigInteger.mod() must be used even if this is	// already an int as the % operator would provide a negative result if	// this is negative, BigInteger.mod() never returns negative values.	if (words != null || isNegative())	  xval = mod(y).ival;	// Swap values so x > y.	if (yval > xval)	  {	    int tmp = xval; xval = yval; yval = tmp;	    swapped = true;	  }	// Normally, the result is in the 2nd element of the array, but	// if originally x < y, then x and y were swapped and the result	// is in the 1st element of the array.	result.ival =	  euclidInv(yval, xval % yval, xval / yval)[swapped ? 0 : 1];	// Result can't be negative, so make it positive by adding the	// original modulus, y.ival (not the possibly "swapped" yval).	if (result.ival < 0)	  result.ival += y.ival;      }    else      {	BigInteger x = this;	// As above, force this to be a positive value via modulo math.	if (isNegative())	  x = mod(y);	// Swap values so x > y.	if (x.compareTo(y) < 0)	  {	    BigInteger tmp = x; x = y; y = tmp;	    swapped = true;	  }	// As above (for ints), result will be in the 2nd element unless	// the original x and y were swapped.	BigInteger rem = new BigInteger();	BigInteger quot = new BigInteger();	divide(x, y, quot, rem, FLOOR);        // quot and rem may not be in canonical form. ensure        rem.canonicalize();        quot.canonicalize();	result = euclidInv(y, rem, quot)[swapped ? 0 : 1];	// Result can't be negative, so make it positive by adding the	// original modulus, y (which is now x if they were swapped).	if (result.isNegative())	  result = add(result, swapped ? x : y, 1);      }        return result;  }