Skip to content

Instantly share code, notes, and snippets.

@olofmagn
Created December 14, 2020 20:54
Show Gist options
  • Save olofmagn/2bfa95edf3c6d2a3748f8eac66e1d0f5 to your computer and use it in GitHub Desktop.
Save olofmagn/2bfa95edf3c6d2a3748f8eac66e1d0f5 to your computer and use it in GitHub Desktop.
import java.math.BigInteger;
import java.util.BitSet;
public class EEA {
public static BigInteger[] EEA(BigInteger a, BigInteger b) {
// Note: as you can see in the test suite,
// your function should work for any (positive) value of a and b.
//We need to redo EEA with BigIntegers from previous Task
BigInteger s = BigInteger.ZERO;
BigInteger t = BigInteger.ONE;
BigInteger x = BigInteger.ONE;
BigInteger y = BigInteger.ZERO;
BigInteger[] result = new BigInteger[3];
if (a.equals(b)) {
result[0] = a; //gcd
result[1] = s;
result[2] = t;
} else {
while (!a.equals(BigInteger.ONE)) {
BigInteger q = a.divide(b);
BigInteger tmp;
tmp = b;
b = a.subtract(q.multiply(b));
a = tmp;
tmp = s;
s = x.subtract(q.multiply(s));
x = tmp;
tmp = t;
t = y.subtract(q.multiply(t));
y = tmp;
}
}
//Assign the semantic values to the array
BigInteger gcd;
gcd = a;
result[0] = gcd;
result[1] = x;
result[2] = y;
//System.out.println(Arrays.toString(result));
return result;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment