Repository: TheAlgorithms/Java
Pull request #5156 (2024-05-11 16:37:45)

refactor: cleanup `DudeneyNumber`
<!--
Thank you for your contribution!
In order to reduce the number of notifications sent to the maintainers, please:
- create your PR as draft, cf. https://docs.github.com/en/pull-requests/collaborating-with-pull-requests/proposing-changes-to-your-work-with-pull-requests/about-pull-requests#draft-pull-requests,
- make sure that all of the CI checks pass,
- mark your PR as ready for review, cf. https://docs.github.com/en/pull-requests/collaborating-with-pull-requests/proposing-changes-to-your-work-with-pull-requests/changing-the-stage-of-a-pull-request#marking-a-pull-request-as-ready-for-review
-->

This PR _cleanups_ `DudeneyNumber` and its tests.

<!-- For completed items, change [ ] to [x] -->

- [x] I have read [CONTRIBUTING.md](https://github.com/TheAlgorithms/Java/blob/master/CONTRIBUTING.md).
- [x] This pull request is all my own work -- I have not plagiarized it.
- [x] All filenames are in PascalCase.
- [x] All functions and variable names follow Java naming conventions.
- [x] All new algorithms have a URL in their comments that points to Wikipedia or other similar explanations.
- [x] All new code is formatted with `clang-format -i --style=file path/to/your/file.java`

codecov-commenter: ## [Codecov](https://app.codecov.io/gh/TheAlgorithms/Java/pull/5156?dropdown=coverage&src=pr&el=h1&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=TheAlgorithms) Report
All modified and coverable lines are covered by tests :white_check_mark:
> Project coverage is 38.25%. Comparing base [(`cf6c87c`)](https://app.codecov.io/gh/TheAlgorithms/Java/commit/cf6c87c35c309e754e6029e135c2801d143ff426?dropdown=coverage&el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=TheAlgorithms) to head [(`8b4b5ae`)](https://app.codecov.io/gh/TheAlgorithms/Java/pull/5156?dropdown=coverage&src=pr&el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=TheAlgorithms).


<details><summary>Additional details and impacted files</summary>


```diff
@@             Coverage Diff              @@
##             master    #5156      +/-   ##
============================================
- Coverage     38.25%   38.25%   -0.01%     
  Complexity     2361     2361              
============================================
  Files           516      516              
  Lines         15309    15304       -5     
  Branches       2958     2957       -1     
============================================
- Hits           5857     5854       -3     
+ Misses         9165     9164       -1     
+ Partials        287      286       -1     
```



</details>

[:umbrella: View full report in Codecov by Sentry](https://app.codecov.io/gh/TheAlgorithms/Java/pull/5156?dropdown=coverage&src=pr&el=continue&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=TheAlgorithms).   
:loudspeaker: Have feedback on the report? [Share it here](https://about.codecov.io/codecov-pr-comment-feedback/?utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=TheAlgorithms).


Patch:
diff --git a/pmd-exclude.properties b/pmd-exclude.properties
index fb8470d8..f2e35f43 100644
--- a/pmd-exclude.properties
+++ b/pmd-exclude.properties
@@ -41,7 +41,6 @@ com.thealgorithms.dynamicprogramming.WineProblem=UselessParentheses
 com.thealgorithms.maths.BinomialCoefficient=UselessParentheses
 com.thealgorithms.maths.Complex=UselessParentheses
 com.thealgorithms.maths.DistanceFormulaTest=UnnecessaryFullyQualifiedName
-com.thealgorithms.maths.DudeneyNumber=UselessParentheses
 com.thealgorithms.maths.FibonacciJavaStreamsTest=BigIntegerInstantiation
 com.thealgorithms.maths.Gaussian=UselessParentheses
 com.thealgorithms.maths.GcdSolutionWrapper=UselessParentheses
diff --git a/src/main/java/com/thealgorithms/maths/DudeneyNumber.java b/src/main/java/com/thealgorithms/maths/DudeneyNumber.java
index c8c8820d..88e008cf 100644
--- a/src/main/java/com/thealgorithms/maths/DudeneyNumber.java
+++ b/src/main/java/com/thealgorithms/maths/DudeneyNumber.java
@@ -11,28 +11,18 @@ public final class DudeneyNumber {
     }
 
     // returns True if the number is a Dudeney number and False if it is not a Dudeney number.
-    public static boolean isDudeney(int n) {
+    public static boolean isDudeney(final int n) {
+        if (n <= 0) {
+            throw new IllegalArgumentException("Input must me positive.");
+        }
         // Calculating Cube Root
-        int cube_root = (int) (Math.round((Math.pow(n, 1.0 / 3.0))));
+        final int cube_root = (int) Math.round(Math.pow(n, 1.0 / 3.0));
         // If the number is not a perfect cube the method returns false.
         if (cube_root * cube_root * cube_root != n) {
             return false;
         }
-        int sum_of_digits = 0; // Stores the sums of the digits of the entered number
-        int temp = n; // A temporary variable to store the entered number
-        // Loop to calculate the sum of the digits.
-        while (temp > 0) {
-            // Extracting the Last digit of the number
-            int rem = temp % 10;
-
-            // Calculating the sum of digits.
-            sum_of_digits += rem;
-
-            // Removing the last digit
-            temp /= 10;
-        }
 
         // If the cube root of the number is not equal to the sum of its digits, we return false.
-        return cube_root == sum_of_digits;
+        return cube_root == SumOfDigits.sumOfDigits(n);
     }
 }


Test patch:
diff --git a/src/test/java/com/thealgorithms/maths/DudeneyNumberTest.java b/src/test/java/com/thealgorithms/maths/DudeneyNumberTest.java
index 718a1def..cd93edfa 100644
--- a/src/test/java/com/thealgorithms/maths/DudeneyNumberTest.java
+++ b/src/test/java/com/thealgorithms/maths/DudeneyNumberTest.java
@@ -1,18 +1,28 @@
 package com.thealgorithms.maths;
 
 import static org.junit.jupiter.api.Assertions.assertFalse;
+import static org.junit.jupiter.api.Assertions.assertThrows;
 import static org.junit.jupiter.api.Assertions.assertTrue;
 
-import org.junit.jupiter.api.Test;
+import org.junit.jupiter.params.ParameterizedTest;
+import org.junit.jupiter.params.provider.CsvSource;
 
 class DudeneyNumberTest {
+    @ParameterizedTest
+    @CsvSource({"1", "512", "4913", "5832", "17576", "19683"})
+    void positiveDudeneyBase10Power3(final int n) {
+        assertTrue(DudeneyNumber.isDudeney(n));
+    }
 
-    @Test
-    void isDudeney() {
-        final int validDudeneyNumber = 512;
-        final int invalidDudeneyNumber = 125;
+    @ParameterizedTest
+    @CsvSource({"2", "19", "21", "125", "27", "343", "729", "19682", "19684"})
+    void negativeDudeneyBase10Power3(final int n) {
+        assertFalse(DudeneyNumber.isDudeney(n));
+    }
 
-        assertTrue(() -> DudeneyNumber.isDudeney(validDudeneyNumber));
-        assertFalse(() -> DudeneyNumber.isDudeney(invalidDudeneyNumber));
+    @ParameterizedTest
+    @CsvSource({"0", "-1"})
+    void throwsInputLessThanOne(final int n) {
+        assertThrows(IllegalArgumentException.class, () -> DudeneyNumber.isDudeney(n));
     }
 }