refactoring

This commit is contained in:
vatsalgosar 2019-08-06 00:21:13 +05:30
parent aa7b95e6a1
commit 88444addaa
1 changed files with 14 additions and 10 deletions

View File

@ -4,26 +4,29 @@ public class GCDImplementation {
public static int gcdByBruteForce(int n1, int n2) {
int gcd = 1;
for (int i = 1; i <= n1 && i <= n2; i++)
if (n1 % i == 0 && n2 % i == 0)
for (int i = 1; i <= n1 && i <= n2; i++) {
if (n1 % i == 0 && n2 % i == 0) {
gcd = i;
}
}
return gcd;
}
public static int gcdByEuclidsAlgorithm(int n1, int n2) {
if (n2 == 0)
if (n2 == 0) {
return n1;
}
return gcdByEuclidsAlgorithm(n2, n1 % n2);
}
public static int gcdBySteinsAlgorithm(int n1, int n2) {
if (n1 == 0)
if (n1 == 0) {
return n2;
}
if (n2 == 0)
if (n2 == 0) {
return n1;
}
int n;
for (n = 0; ((n1 | n2) & 1) == 0; n++) {
@ -31,12 +34,14 @@ public class GCDImplementation {
n2 >>= 1;
}
while ((n1 & 1) == 0)
while ((n1 & 1) == 0) {
n1 >>= 1;
}
do {
while ((n2 & 1) == 0)
while ((n2 & 1) == 0) {
n2 >>= 1;
}
if (n1 > n2) {
int temp = n1;
@ -45,7 +50,6 @@ public class GCDImplementation {
}
n2 = (n2 - n1);
} while (n2 != 0);
return n1 << n;
}
}