mirror of
https://github.com/TheAlgorithms/Java.git
synced 2025-07-11 06:04:27 +08:00
refactor: cleanup DudeneyNumber
(#5156)
This commit is contained in:
@ -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);
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user