summaryrefslogtreecommitdiff
path: root/AlgoDesignAndTechniqueEdxJava/sources
diff options
context:
space:
mode:
authorHaidong Ji2018-08-19 10:24:13 -0500
committerHaidong Ji2018-08-19 10:24:13 -0500
commite56fcf9023ba43919cae20393a2b85d89206271c (patch)
treed20f9544965feb8d7363ff789d1c8002d37a09de /AlgoDesignAndTechniqueEdxJava/sources
parent9633df82de06c9ef6b88bb2068e3fdf19a1554c4 (diff)
GCD done!
Interesting. The original code was like this: public static int getGCD(int a, int b) { if (b == 0) { return a; } if (b > a) { return getGCD(b, b % a); } else { return getGCD(b, a % b); } } And it failed the test of 14159572, 63967072. After chaning the code to: public static int getGCD(int a, int b) { if (b == 0) { return a; } if (b > a) { return getGCD(b, a); } else { return getGCD(b, a % b); } } It worked! I guess it's important to do a pretty exhaustive testing to find this subtle bug
Diffstat (limited to 'AlgoDesignAndTechniqueEdxJava/sources')
-rw-r--r--AlgoDesignAndTechniqueEdxJava/sources/GCD.java24
1 files changed, 24 insertions, 0 deletions
diff --git a/AlgoDesignAndTechniqueEdxJava/sources/GCD.java b/AlgoDesignAndTechniqueEdxJava/sources/GCD.java
new file mode 100644
index 0000000..5e29e4f
--- /dev/null
+++ b/AlgoDesignAndTechniqueEdxJava/sources/GCD.java
@@ -0,0 +1,24 @@
+import java.util.Scanner;
+
+public class GCD {
+
+ public static int getGCD(int a, int b) {
+ if (b == 0) {
+ return a;
+ }
+
+ if (b > a) {
+ return getGCD(b, a);
+ } else {
+ return getGCD(b, a % b);
+ }
+ }
+
+ public static void main(String args[]) {
+ Scanner in = new Scanner(System.in);
+ int a = in.nextInt();
+ int b = in.nextInt();
+
+ System.out.println(getGCD(a, b));
+ }
+}