Skip to content
Closed
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
6 changes: 2 additions & 4 deletions src/main/java/com/thealgorithms/maths/Armstrong.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,16 +21,14 @@ public class Armstrong {
*/
public boolean isArmstrong(int number) {
long sum = 0;
String temp = Integer.toString(number); // Convert the given number to a string
int power = temp.length(); // Extract the length of the number (number of digits)
int totalDigits = (int) Math.log10(number) + 1;; // get the length of the number (number of digits)
long originalNumber = number;

while (originalNumber > 0) {
long digit = originalNumber % 10;
sum += (long) Math.pow(digit, power); // The digit raised to the power of the number of digits and added to the sum.
sum += (long) Math.pow(digit, totalDigits); // The digit raised to the power of the number of digits and added to the sum.
originalNumber /= 10;
}

return sum == number;
}
}
Loading