From cbb583a73eaed5b5d467a7987b9010862a0f5ce4 Mon Sep 17 00:00:00 2001 From: vil02 Date: Mon, 19 Jun 2023 15:58:27 +0200 Subject: [PATCH 1/4] test: add missing test --- .../others/EulersFunctionTest.java | 33 +++++++++++++++++++ 1 file changed, 33 insertions(+) create mode 100644 src/test/java/com/thealgorithms/others/EulersFunctionTest.java diff --git a/src/test/java/com/thealgorithms/others/EulersFunctionTest.java b/src/test/java/com/thealgorithms/others/EulersFunctionTest.java new file mode 100644 index 000000000000..3152d76477ef --- /dev/null +++ b/src/test/java/com/thealgorithms/others/EulersFunctionTest.java @@ -0,0 +1,33 @@ +package com.thealgorithms.others; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import java.util.HashMap; +import org.junit.jupiter.api.Test; + +class EulersFunctionTest { + @Test + public void testGetEuler() { + HashMap testCases = new HashMap<>(); + testCases.put(1, 1); + testCases.put(2, 1); + testCases.put(3, 2); + testCases.put(4, 2); + testCases.put(5, 4); + testCases.put(6, 2); + testCases.put(10, 4); + testCases.put(21, 12); + testCases.put(69, 44); + testCases.put(47, 46); + testCases.put(46, 22); + testCases.put(55, 40); + testCases.put(34, 16); + testCases.put(20, 8); + testCases.put(20, 8); + testCases.put(1024, 512); + + for (final var tc : testCases.entrySet()) { + assertEquals(tc.getValue(), EulersFunction.getEuler(tc.getKey())); + } + } +} From 914dd6cc3c11fb5a3bcb2a5faecc8bd6bf64ebc3 Mon Sep 17 00:00:00 2001 From: vil02 Date: Mon, 19 Jun 2023 16:12:55 +0200 Subject: [PATCH 2/4] feat: throw for inputs <= 0 --- src/main/java/com/thealgorithms/others/EulersFunction.java | 6 ++++++ .../java/com/thealgorithms/others/EulersFunctionTest.java | 6 ++++++ 2 files changed, 12 insertions(+) diff --git a/src/main/java/com/thealgorithms/others/EulersFunction.java b/src/main/java/com/thealgorithms/others/EulersFunction.java index b8ab2acfa087..0d41fc359c11 100644 --- a/src/main/java/com/thealgorithms/others/EulersFunction.java +++ b/src/main/java/com/thealgorithms/others/EulersFunction.java @@ -7,11 +7,17 @@ * See https://en.wikipedia.org/wiki/Euler%27s_totient_function */ public class EulersFunction { + private static void checkInput(int n) { + if (n <= 0) { + throw new IllegalArgumentException("n must be positive."); + } + } // This method returns us number of x that (x < n) and gcd(x, n) == 1 in O(sqrt(n)) time // complexity; public static int getEuler(int n) { + checkInput(n); int result = n; for (int i = 2; i * i <= n; i++) { if (n % i == 0) { diff --git a/src/test/java/com/thealgorithms/others/EulersFunctionTest.java b/src/test/java/com/thealgorithms/others/EulersFunctionTest.java index 3152d76477ef..b80926b8c2dc 100644 --- a/src/test/java/com/thealgorithms/others/EulersFunctionTest.java +++ b/src/test/java/com/thealgorithms/others/EulersFunctionTest.java @@ -1,6 +1,7 @@ package com.thealgorithms.others; import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertThrows; import java.util.HashMap; import org.junit.jupiter.api.Test; @@ -30,4 +31,9 @@ public void testGetEuler() { assertEquals(tc.getValue(), EulersFunction.getEuler(tc.getKey())); } } + + @Test + public void testGetEulerThrowsExceptionForNonPositiveInput() { + assertThrows(IllegalArgumentException.class, () -> EulersFunction.getEuler(0)); + } } From 7a54e2732ecb5935ddc402e31999fe5671521ea0 Mon Sep 17 00:00:00 2001 From: vil02 Date: Mon, 19 Jun 2023 16:26:55 +0200 Subject: [PATCH 3/4] refactor: make EulersFunction a proper utility class --- .../java/com/thealgorithms/others/EulersFunction.java | 11 ++++------- 1 file changed, 4 insertions(+), 7 deletions(-) diff --git a/src/main/java/com/thealgorithms/others/EulersFunction.java b/src/main/java/com/thealgorithms/others/EulersFunction.java index 0d41fc359c11..fc6eb8e4275b 100644 --- a/src/main/java/com/thealgorithms/others/EulersFunction.java +++ b/src/main/java/com/thealgorithms/others/EulersFunction.java @@ -6,7 +6,10 @@ *

* See https://en.wikipedia.org/wiki/Euler%27s_totient_function */ -public class EulersFunction { +final public class EulersFunction { + private EulersFunction() { + } + private static void checkInput(int n) { if (n <= 0) { throw new IllegalArgumentException("n must be positive."); @@ -32,10 +35,4 @@ public static int getEuler(int n) { } return result; } - - public static void main(String[] args) { - for (int i = 1; i < 100; i++) { - System.out.println(getEuler(i)); - } - } } From f32e30c6eba42e6fea90661391ba9dca321646c4 Mon Sep 17 00:00:00 2001 From: vil02 Date: Mon, 19 Jun 2023 16:29:32 +0200 Subject: [PATCH 4/4] docs: update doc-strs --- .../com/thealgorithms/others/EulersFunction.java | 15 ++++++++------- 1 file changed, 8 insertions(+), 7 deletions(-) diff --git a/src/main/java/com/thealgorithms/others/EulersFunction.java b/src/main/java/com/thealgorithms/others/EulersFunction.java index fc6eb8e4275b..27c9aed8b620 100644 --- a/src/main/java/com/thealgorithms/others/EulersFunction.java +++ b/src/main/java/com/thealgorithms/others/EulersFunction.java @@ -1,10 +1,7 @@ package com.thealgorithms.others; /** - * You can read more about Euler's totient function - * - *

- * See https://en.wikipedia.org/wiki/Euler%27s_totient_function + * @brief utility class for Euler's totient function */ final public class EulersFunction { private EulersFunction() { @@ -16,9 +13,13 @@ private static void checkInput(int n) { } } - // This method returns us number of x that (x < n) and gcd(x, n) == 1 in O(sqrt(n)) time - // complexity; - + /** + * @brief computes the value of Euler's totient function for given input + * @details has time complexity of O(sqrt(n)) + * @param n the input + * @exception IllegalArgumentException n is non-positive + * @return the value of Euler's totient function for the input + */ public static int getEuler(int n) { checkInput(n); int result = n;