From 3ff81a894315d4553354d40c7c6589aeb3bdc817 Mon Sep 17 00:00:00 2001 From: sampadawagde Date: Wed, 18 Mar 2020 17:13:37 +0530 Subject: [PATCH] JAVA-626 : Moved extra articles from java-math module --- java-math-2/README.md | 3 +- java-math-2/pom.xml | 14 +++++ .../algorithms/gcd/GCDImplementation.java | 55 +++++++++++++++++++ .../gcd/GCDImplementationUnitTest.java | 32 +++++++++++ .../logarithm/LogarithmUnitTest.java | 31 +++++++++++ 5 files changed, 134 insertions(+), 1 deletion(-) create mode 100644 java-math-2/src/main/java/com/baeldung/algorithms/gcd/GCDImplementation.java create mode 100644 java-math-2/src/test/java/com/baeldung/algorithms/gcd/GCDImplementationUnitTest.java create mode 100644 java-math-2/src/test/java/com/baeldung/algorithms/logarithm/LogarithmUnitTest.java diff --git a/java-math-2/README.md b/java-math-2/README.md index ca809e8623..5345564d5d 100644 --- a/java-math-2/README.md +++ b/java-math-2/README.md @@ -4,5 +4,6 @@ This module contains articles about math in Java. ### Relevant articles: -- [Basic Calculator in Java](https://www.baeldung.com/basic-calculator-in-java) +- [Calculating Logarithms in Java](https://www.baeldung.com/java-logarithms) +- [Finding Greatest Common Divisor in Java](https://www.baeldung.com/java-greatest-common-divisor) - More articles: [[<-- prev]](/../java-math) diff --git a/java-math-2/pom.xml b/java-math-2/pom.xml index e22dcc2b31..7bc05a3a86 100644 --- a/java-math-2/pom.xml +++ b/java-math-2/pom.xml @@ -12,4 +12,18 @@ 1.0.0-SNAPSHOT + + + org.assertj + assertj-core + ${org.assertj.core.version} + test + + + + + + 3.9.0 + + \ No newline at end of file diff --git a/java-math-2/src/main/java/com/baeldung/algorithms/gcd/GCDImplementation.java b/java-math-2/src/main/java/com/baeldung/algorithms/gcd/GCDImplementation.java new file mode 100644 index 0000000000..d4844abd9c --- /dev/null +++ b/java-math-2/src/main/java/com/baeldung/algorithms/gcd/GCDImplementation.java @@ -0,0 +1,55 @@ +package com.baeldung.algorithms.gcd; + +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) { + gcd = i; + } + } + return gcd; + } + + public static int gcdByEuclidsAlgorithm(int n1, int n2) { + if (n2 == 0) { + return n1; + } + return gcdByEuclidsAlgorithm(n2, n1 % n2); + } + + public static int gcdBySteinsAlgorithm(int n1, int n2) { + if (n1 == 0) { + return n2; + } + + if (n2 == 0) { + return n1; + } + + int n; + for (n = 0; ((n1 | n2) & 1) == 0; n++) { + n1 >>= 1; + n2 >>= 1; + } + + while ((n1 & 1) == 0) { + n1 >>= 1; + } + + do { + while ((n2 & 1) == 0) { + n2 >>= 1; + } + + if (n1 > n2) { + int temp = n1; + n1 = n2; + n2 = temp; + } + n2 = (n2 - n1); + } while (n2 != 0); + return n1 << n; + } +} diff --git a/java-math-2/src/test/java/com/baeldung/algorithms/gcd/GCDImplementationUnitTest.java b/java-math-2/src/test/java/com/baeldung/algorithms/gcd/GCDImplementationUnitTest.java new file mode 100644 index 0000000000..d2c91a2eb8 --- /dev/null +++ b/java-math-2/src/test/java/com/baeldung/algorithms/gcd/GCDImplementationUnitTest.java @@ -0,0 +1,32 @@ +package com.baeldung.algorithms.gcd; + +import org.junit.Test; + +import static org.assertj.core.api.Assertions.assertThat; + +public class GCDImplementationUnitTest { + + @Test + public void whenCalculatingGCDByBruteForceMethod_thenCorrect() { + int n1 = 60; + int n2 = 90; + int gcd = GCDImplementation.gcdByBruteForce(n1, n2); + assertThat(gcd).isEqualTo(30); + } + + @Test + public void whenCalculatingGCDByEuclidsAlgorithm_thenCorrect() { + int n1 = 60; + int n2 = 90; + int gcd = GCDImplementation.gcdByEuclidsAlgorithm(n1, n2); + assertThat(gcd).isEqualTo(30); + } + + @Test + public void whenCalculatingGCDBySteinsAlgorithm_thenCorrect() { + int n1 = 60; + int n2 = 90; + int gcd = GCDImplementation.gcdBySteinsAlgorithm(n1, n2); + assertThat(gcd).isEqualTo(30); + } +} diff --git a/java-math-2/src/test/java/com/baeldung/algorithms/logarithm/LogarithmUnitTest.java b/java-math-2/src/test/java/com/baeldung/algorithms/logarithm/LogarithmUnitTest.java new file mode 100644 index 0000000000..facad1edc4 --- /dev/null +++ b/java-math-2/src/test/java/com/baeldung/algorithms/logarithm/LogarithmUnitTest.java @@ -0,0 +1,31 @@ +package com.baeldung.algorithms.logarithm; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import org.junit.Test; + +public class LogarithmUnitTest { + + @Test + public void givenLog10_shouldReturnValidResults() { + assertEquals(Math.log10(100), 2); + assertEquals(Math.log10(1000), 3); + } + + @Test + public void givenLogE_shouldReturnValidResults() { + assertEquals(Math.log(Math.E), 1); + assertEquals(Math.log(10), 2.30258, 0.00001); + } + + @Test + public void givenCustomLog_shouldReturnValidResults() { + assertEquals(customLog(2, 256), 8); + assertEquals(customLog(10, 100), 2); + } + + private static double customLog(double base, double logNumber) { + return Math.log(logNumber) / Math.log(base); + } + +}