Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: cleanup DudeneyNumber #5156

Merged
merged 1 commit into from
May 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion pmd-exclude.properties
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
22 changes: 6 additions & 16 deletions src/main/java/com/thealgorithms/maths/DudeneyNumber.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,28 +11,18 @@ private 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);
}
}
24 changes: 17 additions & 7 deletions src/test/java/com/thealgorithms/maths/DudeneyNumberTest.java
Original file line number Diff line number Diff line change
@@ -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));
}
}