Skip to content

Commit 319d514

Browse files
authored
refactor: cleanup DudeneyNumber (#5156)
1 parent cf6c87c commit 319d514

File tree

3 files changed

+23
-24
lines changed

3 files changed

+23
-24
lines changed

pmd-exclude.properties

-1
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,6 @@ com.thealgorithms.dynamicprogramming.WineProblem=UselessParentheses
4141
com.thealgorithms.maths.BinomialCoefficient=UselessParentheses
4242
com.thealgorithms.maths.Complex=UselessParentheses
4343
com.thealgorithms.maths.DistanceFormulaTest=UnnecessaryFullyQualifiedName
44-
com.thealgorithms.maths.DudeneyNumber=UselessParentheses
4544
com.thealgorithms.maths.FibonacciJavaStreamsTest=BigIntegerInstantiation
4645
com.thealgorithms.maths.Gaussian=UselessParentheses
4746
com.thealgorithms.maths.GcdSolutionWrapper=UselessParentheses

src/main/java/com/thealgorithms/maths/DudeneyNumber.java

+6-16
Original file line numberDiff line numberDiff line change
@@ -11,28 +11,18 @@ private DudeneyNumber() {
1111
}
1212

1313
// returns True if the number is a Dudeney number and False if it is not a Dudeney number.
14-
public static boolean isDudeney(int n) {
14+
public static boolean isDudeney(final int n) {
15+
if (n <= 0) {
16+
throw new IllegalArgumentException("Input must me positive.");
17+
}
1518
// Calculating Cube Root
16-
int cube_root = (int) (Math.round((Math.pow(n, 1.0 / 3.0))));
19+
final int cube_root = (int) Math.round(Math.pow(n, 1.0 / 3.0));
1720
// If the number is not a perfect cube the method returns false.
1821
if (cube_root * cube_root * cube_root != n) {
1922
return false;
2023
}
21-
int sum_of_digits = 0; // Stores the sums of the digits of the entered number
22-
int temp = n; // A temporary variable to store the entered number
23-
// Loop to calculate the sum of the digits.
24-
while (temp > 0) {
25-
// Extracting the Last digit of the number
26-
int rem = temp % 10;
27-
28-
// Calculating the sum of digits.
29-
sum_of_digits += rem;
30-
31-
// Removing the last digit
32-
temp /= 10;
33-
}
3424

3525
// If the cube root of the number is not equal to the sum of its digits, we return false.
36-
return cube_root == sum_of_digits;
26+
return cube_root == SumOfDigits.sumOfDigits(n);
3727
}
3828
}
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,28 @@
11
package com.thealgorithms.maths;
22

33
import static org.junit.jupiter.api.Assertions.assertFalse;
4+
import static org.junit.jupiter.api.Assertions.assertThrows;
45
import static org.junit.jupiter.api.Assertions.assertTrue;
56

6-
import org.junit.jupiter.api.Test;
7+
import org.junit.jupiter.params.ParameterizedTest;
8+
import org.junit.jupiter.params.provider.CsvSource;
79

810
class DudeneyNumberTest {
11+
@ParameterizedTest
12+
@CsvSource({"1", "512", "4913", "5832", "17576", "19683"})
13+
void positiveDudeneyBase10Power3(final int n) {
14+
assertTrue(DudeneyNumber.isDudeney(n));
15+
}
916

10-
@Test
11-
void isDudeney() {
12-
final int validDudeneyNumber = 512;
13-
final int invalidDudeneyNumber = 125;
17+
@ParameterizedTest
18+
@CsvSource({"2", "19", "21", "125", "27", "343", "729", "19682", "19684"})
19+
void negativeDudeneyBase10Power3(final int n) {
20+
assertFalse(DudeneyNumber.isDudeney(n));
21+
}
1422

15-
assertTrue(() -> DudeneyNumber.isDudeney(validDudeneyNumber));
16-
assertFalse(() -> DudeneyNumber.isDudeney(invalidDudeneyNumber));
23+
@ParameterizedTest
24+
@CsvSource({"0", "-1"})
25+
void throwsInputLessThanOne(final int n) {
26+
assertThrows(IllegalArgumentException.class, () -> DudeneyNumber.isDudeney(n));
1727
}
1828
}

0 commit comments

Comments
 (0)