From 92b4bc0aa709f5eaf96e2e4a9d670af61f329f04 Mon Sep 17 00:00:00 2001 From: xam4lor Date: Fri, 29 Jun 2018 23:22:17 +0200 Subject: [PATCH 1/3] Modified and cleaned last Java example --- .../code/java/EuclideanAlgo.java | 28 +++++++++++++++ .../code/java/MainClass.java | 8 +++++ .../code/java/euclidean_example.java | 36 ------------------- chapters/euclidean_algorithm/euclidean.md | 17 +++++---- 4 files changed, 46 insertions(+), 43 deletions(-) create mode 100644 chapters/euclidean_algorithm/code/java/EuclideanAlgo.java create mode 100644 chapters/euclidean_algorithm/code/java/MainClass.java delete mode 100644 chapters/euclidean_algorithm/code/java/euclidean_example.java diff --git a/chapters/euclidean_algorithm/code/java/EuclideanAlgo.java b/chapters/euclidean_algorithm/code/java/EuclideanAlgo.java new file mode 100644 index 000000000..1f4ff62f7 --- /dev/null +++ b/chapters/euclidean_algorithm/code/java/EuclideanAlgo.java @@ -0,0 +1,28 @@ +// submitted by lolatomroflsinnlos, modified by xam4lor +public class EuclideanAlgo { + public static int euclidSub(int a, int b) { + a = Math.abs(a); + b = Math.abs(b); + + while (a != b) { + if (a > b) { + a -= b; + } + else { + b -= a; + } + } + + return a; + } + + public static int euclidMod(int a, int b) { + while (b != 0) { + int tmp = b; + b = a % b; + a = tmp; + } + + return a; + } +} diff --git a/chapters/euclidean_algorithm/code/java/MainClass.java b/chapters/euclidean_algorithm/code/java/MainClass.java new file mode 100644 index 000000000..0f36f60fb --- /dev/null +++ b/chapters/euclidean_algorithm/code/java/MainClass.java @@ -0,0 +1,8 @@ +//submitted by lolatomroflsinnlos, modified by xam4lor +public class MainClass { + public static void main(String[] args) { + System.out.println("Euclidean Algorithm :"); + System.out.println(EuclideanAlgo.euclidMod(64 * 67, 64 * 81)); + System.out.println(EuclideanAlgo.euclidSub(128 * 12, 128 * 77) + "\n"); + } +} diff --git a/chapters/euclidean_algorithm/code/java/euclidean_example.java b/chapters/euclidean_algorithm/code/java/euclidean_example.java deleted file mode 100644 index e05500574..000000000 --- a/chapters/euclidean_algorithm/code/java/euclidean_example.java +++ /dev/null @@ -1,36 +0,0 @@ -// sumbitted by lolatomroflsinnlos -public static void main(String[] args) { - - System.out.println(euclidSub(64 * 67, 64 * 81)); - System.out.println(euclidMod(128 * 12, 128 * 77)); - -} - -public static int euclidSub(int a, int b) { - a = Math.abs(a); - b = Math.abs(b); - - while (a != b) { - if (a > b) { - a -=b; - } else { - b -=a; - } - } - - return a; -} - -public static int euclidMod(int a, int b) { - a = Math.abs(a); - b = Math.abs(b); - - while (b != 0){ - int temp = b; - b = a % b; - a = temp; - } - - return a; -} - diff --git a/chapters/euclidean_algorithm/euclidean.md b/chapters/euclidean_algorithm/euclidean.md index 3530cdcbf..2c982e51c 100644 --- a/chapters/euclidean_algorithm/euclidean.md +++ b/chapters/euclidean_algorithm/euclidean.md @@ -13,6 +13,8 @@ The algorithm is a simple way to find the *greatest common divisor* (GCD) of two [import:2-8, lang="clojure"](code/clojure/euclidean_example.clj) {% sample lang="cpp" %} [import:20-33, lang="c_cpp"](code/c++/euclidean.cpp) +{% sample lang="java" %} +[import:3-17, lang="java"](code/java/EuclideanAlgo.java) {% sample lang="js" %} [import:15-29, lang="javascript"](code/javascript/euclidean_example.js) {% sample lang="py2" %} @@ -23,8 +25,6 @@ The algorithm is a simple way to find the *greatest common divisor* (GCD) of two [import:3-15, lang="rust"](code/rust/euclidean_example.rs) {% sample lang="ml" %} [import:9-17, lang="ocaml"](code/ocaml/euclidean_example.ml) -{% sample lang="java" %} -[import:9-22, lang="java"](code/java/euclidean_example.java) {% endmethod %} Here, we simply line the two numbers up every step and subtract the lower value from the higher one every timestep. Once the two values are equal, we call that value the greatest common divisor. A graph of `a` and `b` as they change every step would look something like this: @@ -44,6 +44,8 @@ Modern implementations, though, often use the modulus operator (%) like so [import:9-13, lang="clojure"](code/clojure/euclidean_example.clj) {% sample lang="cpp" %} [import:7-17, lang="c_cpp"](code/c++/euclidean.cpp) +{% sample lang="java" %} +[import:19-27, lang="java"](code/java/EuclideanAlgo.java) {% sample lang="js" %} [import:1-13, lang="javascript"](code/javascript/euclidean_example.js) {% sample lang="py2" %} @@ -54,8 +56,6 @@ Modern implementations, though, often use the modulus operator (%) like so [import:17-27, lang="rust"](code/rust/euclidean_example.rs) {% sample lang="ml" %} [import:3-7, lang="ocaml"](code/ocaml/euclidean_example.ml) -{% sample lang="java" %} -[import:24-35, lang="java"](code/java/euclidean_example.java) {% endmethod %} Here, we set `b` to be the remainder of `a%b` and `a` to be whatever `b` was last timestep. Because of how the modulus operator works, this will provide the same information as the subtraction-based implementation, but when we show `a` and `b` as they change with time, we can see that it might take many fewer steps: @@ -84,6 +84,12 @@ Program.cs {% sample lang="cpp" %} ### Cpp [import, lang="c_cpp"](code/c++/euclidean.cpp) +{% sample lang="java" %} +### Java +EuclideanAlgo.java +[import, lang="java"](code/java/EuclideanAlgo.java) +MainClass.java +[import, lang="java"](code/java/MainClass.java) {% sample lang="js" %} ### JavaScript [import, lang="javascript"](code/javascript/euclidean_example.js) @@ -99,9 +105,6 @@ Program.cs {% sample lang="ml" %} ### Ocaml [import, lang="ocaml"](code/ocaml/euclidean_example.ml) -{% sample lang="java" %} -### Java -[import, lang="java"](code/java/euclidean_example.java) {% endmethod %} From d47c9eee18c5e989f7aae9559384d689a8c3cc3d Mon Sep 17 00:00:00 2001 From: xam4lor Date: Sat, 30 Jun 2018 10:27:21 +0200 Subject: [PATCH 2/3] Update EuclideanAlgo.java --- chapters/euclidean_algorithm/code/java/EuclideanAlgo.java | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/chapters/euclidean_algorithm/code/java/EuclideanAlgo.java b/chapters/euclidean_algorithm/code/java/EuclideanAlgo.java index 1f4ff62f7..ecf5e470f 100644 --- a/chapters/euclidean_algorithm/code/java/EuclideanAlgo.java +++ b/chapters/euclidean_algorithm/code/java/EuclideanAlgo.java @@ -7,8 +7,7 @@ public static int euclidSub(int a, int b) { while (a != b) { if (a > b) { a -= b; - } - else { + } else { b -= a; } } From 91491586b29e58bb0d05d78de1e6af0e478b86c4 Mon Sep 17 00:00:00 2001 From: xam4lor Date: Sat, 30 Jun 2018 10:28:19 +0200 Subject: [PATCH 3/3] Update euclidean.md --- chapters/euclidean_algorithm/euclidean.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/chapters/euclidean_algorithm/euclidean.md b/chapters/euclidean_algorithm/euclidean.md index f35e44264..200001c39 100644 --- a/chapters/euclidean_algorithm/euclidean.md +++ b/chapters/euclidean_algorithm/euclidean.md @@ -14,7 +14,7 @@ The algorithm is a simple way to find the *greatest common divisor* (GCD) of two {% sample lang="cpp" %} [import:20-33, lang="c_cpp"](code/c++/euclidean.cpp) {% sample lang="java" %} -[import:3-17, lang="java"](code/java/EuclideanAlgo.java) +[import:3-16, lang="java"](code/java/EuclideanAlgo.java) {% sample lang="js" %} [import:15-29, lang="javascript"](code/javascript/euclidean_example.js) {% sample lang="py" %} @@ -47,7 +47,7 @@ Modern implementations, though, often use the modulus operator (%) like so {% sample lang="cpp" %} [import:7-17, lang="c_cpp"](code/c++/euclidean.cpp) {% sample lang="java" %} -[import:19-27, lang="java"](code/java/EuclideanAlgo.java) +[import:18-26, lang="java"](code/java/EuclideanAlgo.java) {% sample lang="js" %} [import:1-13, lang="javascript"](code/javascript/euclidean_example.js) {% sample lang="py" %}