Gcd { public static BigInteger[] of(BigInteger a, BigInteger b) { BigInteger x0 = BigInteger.ONE; BigInteger y0 = BigInteger.ZERO; BigInteger x1 = BigInteger.ZERO; BigInteger y1 = BigInteger.ONE; while(!b.equals(BigInteger.ZERO)) { BigInteger[] divmod = a.divideAndRemainder(b); BigInteger xt = x0; BigInteger yt = y0; x0 = x1; y0 = y1; x1 = xt.subtract(divmod[0].multiply(x1)); y1 = yt.subtract(divmod[0].multiply(y1)); a = b; b = divmod[1]; } return new BigInteger[]{a, x0, y0}; }  static BigInteger[] of(BigInteger a, BigInteger b);  }
@Test public void shouldCalculateTheExtendedGcd() { BigInteger[] result = Gcd.of(a, b); assertEquals(expected, result[0]); assertEquals(result[0], result[1].multiply(a).add(result[2].multiply(b))); }