1
0
forked from kalmar/DALGLI0
DALGLI0/exersize1dommro1/EclideamAlgorithm.java
2018-05-30 23:45:03 +00:00

48 lines
1.3 KiB
Java

public class EclideamAlgorithm {
private int nwd;
private int combinationOfA;
private int combinationOfB;
public EclideamAlgorithm(int numberA, int numberB) {
int[][] matrix = new int[2][3];
matrix[0][0] = 1;
matrix[1][1] = 1;
matrix[0][2] = numberA;
matrix[1][2] = numberB;
while (matrix[0][2] != 0 && matrix[1][2] != 0) {
if (matrix[0][2] > matrix[1][2]) {
matrix[0][0] = matrix[0][0] - matrix[1][0];
matrix[0][1] = matrix[0][1] - matrix[1][1];
matrix[0][2] = matrix[0][2] - matrix[1][2];
} else {
matrix[1][0] = matrix[1][0] - matrix[0][0];
matrix[1][1] = matrix[1][1] - matrix[0][1];
matrix[1][2] = matrix[1][2] - matrix[0][2];
}
}
if (matrix[0][2] != 0) {
nwd = matrix[0][2];
combinationOfA = matrix[0][0];
combinationOfB = matrix[0][1];
} else {
nwd = matrix[1][2];
combinationOfA = matrix[1][0];
combinationOfB = matrix[1][1];
}
}
public int getGCD() {
return nwd;
}
public int getCombinationOfA() {
return combinationOfA;
}
public int getCombinationOfB() {
return combinationOfB;
}
}