File tree 1 file changed +2
-4
lines changed
src/main/java/com/thealgorithms/maths 1 file changed +2
-4
lines changed Original file line number Diff line number Diff line change @@ -21,16 +21,14 @@ public class Armstrong {
21
21
*/
22
22
public boolean isArmstrong (int number ) {
23
23
long sum = 0 ;
24
- String temp = Integer .toString (number ); // Convert the given number to a string
25
- int power = temp .length (); // Extract the length of the number (number of digits)
24
+ int totalDigits = (int ) Math .log10 (number ) + 1 ;; // get the length of the number (number of digits)
26
25
long originalNumber = number ;
27
26
28
27
while (originalNumber > 0 ) {
29
28
long digit = originalNumber % 10 ;
30
- sum += (long ) Math .pow (digit , power ); // The digit raised to the power of the number of digits and added to the sum.
29
+ sum += (long ) Math .pow (digit , totalDigits ); // The digit raised to the power of the number of digits and added to the sum.
31
30
originalNumber /= 10 ;
32
31
}
33
-
34
32
return sum == number ;
35
33
}
36
34
}
You can’t perform that action at this time.
0 commit comments