Made changes to the code to correct the Logic of Armstrong Number (#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
This commit is contained in:
parent
4fab7adfaa
commit
a3a2d845d5
@ -1,29 +1,36 @@
|
|||||||
package com.thealgorithms.maths;
|
package com.thealgorithms.maths;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* An Armstrong number is equal to the sum of the cubes of its digits. For
|
* This class checks whether a given number is an Armstrong number or not.
|
||||||
* example, 370 is an Armstrong number because 3*3*3 + 7*7*7 + 0*0*0 = 370. An
|
* An Armstrong number is a number that is equal to the sum of its own digits,
|
||||||
* Armstrong number is often called Narcissistic number.
|
* each raised to the power of the number of digits.
|
||||||
*
|
*
|
||||||
* @author Vivek
|
* For example, 370 is an Armstrong number because 3^3 + 7^3 + 0^3 = 370.
|
||||||
|
* 1634 is an Armstrong number because 1^4 + 6^4 + 3^4 + 4^4 = 1634.
|
||||||
|
* An Armstrong number is often called a Narcissistic number.
|
||||||
|
*
|
||||||
|
* @author satyabarghav
|
||||||
*/
|
*/
|
||||||
public class Armstrong {
|
public class Armstrong {
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Checks whether a given number is an armstrong number or not.
|
* Checks whether a given number is an Armstrong number or not.
|
||||||
*
|
*
|
||||||
* @param number number to check
|
* @param number the number to check
|
||||||
* @return {@code true} if given number is armstrong number, {@code false}
|
* @return {@code true} if the given number is an Armstrong number, {@code false} otherwise
|
||||||
* otherwise
|
|
||||||
*/
|
*/
|
||||||
public boolean isArmstrong(int number) {
|
public boolean isArmstrong(int number) {
|
||||||
long sum = 0;
|
long sum = 0;
|
||||||
long number2 = number;
|
String temp = Integer.toString(number); // Convert the given number to a string
|
||||||
while (number2 > 0) {
|
int power = temp.length(); // Extract the length of the number (number of digits)
|
||||||
long mod = number2 % 10;
|
long originalNumber = number;
|
||||||
sum += Math.pow(mod, 3);
|
|
||||||
number2 /= 10;
|
while (originalNumber > 0) {
|
||||||
|
long digit = originalNumber % 10;
|
||||||
|
sum += Math.pow(digit, power); // The digit raised to the power of the number of digits and added to the sum.
|
||||||
|
originalNumber /= 10;
|
||||||
}
|
}
|
||||||
|
|
||||||
return sum == number;
|
return sum == number;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -5,8 +5,8 @@ import static org.assertj.core.api.Assertions.assertThat;
|
|||||||
import org.junit.jupiter.api.Test;
|
import org.junit.jupiter.api.Test;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @author Vivek
|
* @author satyabarghav
|
||||||
* @since 15/03/22
|
* @since 4/10/2023
|
||||||
*/
|
*/
|
||||||
class ArmstrongTest {
|
class ArmstrongTest {
|
||||||
|
|
||||||
@ -17,7 +17,9 @@ class ArmstrongTest {
|
|||||||
assertThat(armstrong.isArmstrong(1)).isTrue();
|
assertThat(armstrong.isArmstrong(1)).isTrue();
|
||||||
assertThat(armstrong.isArmstrong(153)).isTrue();
|
assertThat(armstrong.isArmstrong(153)).isTrue();
|
||||||
assertThat(armstrong.isArmstrong(371)).isTrue();
|
assertThat(armstrong.isArmstrong(371)).isTrue();
|
||||||
assertThat(armstrong.isArmstrong(1634)).isFalse();
|
assertThat(armstrong.isArmstrong(1634)).isTrue();
|
||||||
assertThat(armstrong.isArmstrong(200)).isFalse();
|
assertThat(armstrong.isArmstrong(200)).isFalse();
|
||||||
|
assertThat(armstrong.isArmstrong(548834)).isTrue();
|
||||||
|
assertThat(armstrong.isArmstrong(9474)).isTrue();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user