diff --git a/chapters/euclidean_algorithm/code/java/EuclideanAlgo.java b/chapters/euclidean_algorithm/code/java/EuclideanAlgo.java new file mode 100644 index 000000000..ecf5e470f --- /dev/null +++ b/chapters/euclidean_algorithm/code/java/EuclideanAlgo.java @@ -0,0 +1,27 @@ +// 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 2e2db0fc5..200001c39 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-16, lang="java"](code/java/EuclideanAlgo.java) {% sample lang="js" %} [import:15-29, lang="javascript"](code/javascript/euclidean_example.js) {% sample lang="py" %} @@ -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) {% sample lang="go" %} [import:25-38, lang="go"](code/go/euclidean.go) {% endmethod %} @@ -46,6 +46,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:18-26, lang="java"](code/java/EuclideanAlgo.java) {% sample lang="js" %} [import:1-13, lang="javascript"](code/javascript/euclidean_example.js) {% sample lang="py" %} @@ -56,8 +58,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) {% sample lang="go" %} [import:14-23, lang="go"](code/go/euclidean.go) {% endmethod %} @@ -88,6 +88,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) @@ -103,10 +109,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) -{% sample lang="go" %} ### Go [import, lang="go"](code/go/euclidean.go) {% endmethod %}