Skip to content

Commit a3a2d84

Browse files
authored
Made changes to the code to correct the Logic of Armstrong Number (TheAlgorithms#4619)
* Made changes to the code to correct the Logic of Armstrong Number * Resolved the issues * Trying to resolve the Linter error by changing Variable name * Changed Variable Names : trying to resolve Clang error
1 parent 4fab7ad commit a3a2d84

File tree

2 files changed

+25
-16
lines changed

2 files changed

+25
-16
lines changed
Lines changed: 20 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,36 @@
11
package com.thealgorithms.maths;
22

33
/**
4-
* An Armstrong number is equal to the sum of the cubes of its digits. For
5-
* example, 370 is an Armstrong number because 3*3*3 + 7*7*7 + 0*0*0 = 370. An
6-
* Armstrong number is often called Narcissistic number.
4+
* This class checks whether a given number is an Armstrong number or not.
5+
* An Armstrong number is a number that is equal to the sum of its own digits,
6+
* each raised to the power of the number of digits.
77
*
8-
* @author Vivek
8+
* For example, 370 is an Armstrong number because 3^3 + 7^3 + 0^3 = 370.
9+
* 1634 is an Armstrong number because 1^4 + 6^4 + 3^4 + 4^4 = 1634.
10+
* An Armstrong number is often called a Narcissistic number.
11+
*
12+
* @author satyabarghav
913
*/
1014
public class Armstrong {
1115

1216
/**
13-
* Checks whether a given number is an armstrong number or not.
17+
* Checks whether a given number is an Armstrong number or not.
1418
*
15-
* @param number number to check
16-
* @return {@code true} if given number is armstrong number, {@code false}
17-
* otherwise
19+
* @param number the number to check
20+
* @return {@code true} if the given number is an Armstrong number, {@code false} otherwise
1821
*/
1922
public boolean isArmstrong(int number) {
2023
long sum = 0;
21-
long number2 = number;
22-
while (number2 > 0) {
23-
long mod = number2 % 10;
24-
sum += Math.pow(mod, 3);
25-
number2 /= 10;
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)
26+
long originalNumber = number;
27+
28+
while (originalNumber > 0) {
29+
long digit = originalNumber % 10;
30+
sum += Math.pow(digit, power); // The digit raised to the power of the number of digits and added to the sum.
31+
originalNumber /= 10;
2632
}
33+
2734
return sum == number;
2835
}
2936
}

src/test/java/com/thealgorithms/maths/ArmstrongTest.java

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@
55
import org.junit.jupiter.api.Test;
66

77
/**
8-
* @author Vivek
9-
* @since 15/03/22
8+
* @author satyabarghav
9+
* @since 4/10/2023
1010
*/
1111
class ArmstrongTest {
1212

@@ -17,7 +17,9 @@ void testIsArmstrong() {
1717
assertThat(armstrong.isArmstrong(1)).isTrue();
1818
assertThat(armstrong.isArmstrong(153)).isTrue();
1919
assertThat(armstrong.isArmstrong(371)).isTrue();
20-
assertThat(armstrong.isArmstrong(1634)).isFalse();
20+
assertThat(armstrong.isArmstrong(1634)).isTrue();
2121
assertThat(armstrong.isArmstrong(200)).isFalse();
22+
assertThat(armstrong.isArmstrong(548834)).isTrue();
23+
assertThat(armstrong.isArmstrong(9474)).isTrue();
2224
}
2325
}

0 commit comments

Comments
 (0)