From 88444addaa5c9b32a0e4983ace06fb0d9eef1d3e Mon Sep 17 00:00:00 2001 From: vatsalgosar Date: Tue, 6 Aug 2019 00:21:13 +0530 Subject: [PATCH] refactoring --- .../algorithms/gcd/GCDImplementation.java | 24 +++++++++++-------- 1 file changed, 14 insertions(+), 10 deletions(-) diff --git a/java-math/src/main/java/com/baeldung/algorithms/gcd/GCDImplementation.java b/java-math/src/main/java/com/baeldung/algorithms/gcd/GCDImplementation.java index df12c97162..d4844abd9c 100644 --- a/java-math/src/main/java/com/baeldung/algorithms/gcd/GCDImplementation.java +++ b/java-math/src/main/java/com/baeldung/algorithms/gcd/GCDImplementation.java @@ -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; } }