Skip to content

Commit 7003ba8

Browse files
feat: improve the Armstrong Number algorithm (TheAlgorithms#2480)
Co-authored-by: realstealthninja <[email protected]>
1 parent 37aae7c commit 7003ba8

File tree

1 file changed

+75
-21
lines changed

1 file changed

+75
-21
lines changed
Lines changed: 75 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,39 +1,93 @@
1-
// Program to check whether a number is an armstrong number or not
2-
#include <cmath>
3-
#include <iostream>
4-
using std::cin;
5-
using std::cout;
1+
/**
2+
* @file
3+
* @brief Checks whether a number is an [Armstrong
4+
* Number](https://en.wikipedia.org/wiki/Narcissistic_number) or not.
5+
*
6+
* @details
7+
* An Armstrong number is a number that is the sum of its own digits each raised
8+
* to the power of the number of digits. For example: 153 is an Armstrong number
9+
* since 153 = 1^3 + 5^3 + 3^3.
10+
*
11+
* A few examples of valid armstrong numbers:
12+
* 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 153, 370, 371, 407, 1634, 8208, 9474, 54748,
13+
* 92727, 93084.
14+
*
15+
* Armstrong numbers are also known as Narcissistic Numbers, as stated in
16+
* Wikipedia.
17+
*
18+
* @author [Shivam Singhal](https://github.com/shivhek25)
19+
* @author [David Leal](https://github.com/Panquesito7)
20+
*/
621

7-
int main() {
8-
int n = 0, temp = 0, rem = 0, count = 0, sum = 0;
9-
cout << "Enter a number: ";
10-
cin >> n;
22+
#include <cassert> /// for assert
23+
#include <cmath> /// for std::pow
24+
#include <iostream> /// for IO operations
25+
26+
/**
27+
* @namespace
28+
* @brief Dynamic Programming algorithms
29+
*/
30+
namespace dynamic_programming {
1131

12-
temp = n;
32+
/**
33+
* @brief Checks if the given number is armstrong or not.
34+
* @param number the number to check
35+
* @returns false if the given number is NOT armstrong
36+
* @returns true if the given number IS armstrong
37+
*/
38+
template <typename T>
39+
bool is_armstrong(const T &number) {
40+
int count = 0, temp = number, result = 0, rem = 0;
1341

14-
/* First Count the number of digits
15-
in the given number */
42+
// Count the number of digits of the given number.
43+
// For example: 153 would be 3 digits.
1644
while (temp != 0) {
1745
temp /= 10;
1846
count++;
1947
}
2048

21-
/* Calculation for checking of armstrongs number i.e.
22-
in an n-digit number sum of the digits is raised to a power of n
23-
is equal to the original number */
24-
25-
temp = n;
49+
// Calculation for checking of armstrongs number i.e.
50+
// in an n-digit number sum of the digits is raised to a power of `n` is
51+
// equal to the original number.
52+
temp = number;
2653
while (temp != 0) {
2754
rem = temp % 10;
28-
sum += static_cast<int>(pow(rem, count));
55+
result += static_cast<T>(std::pow(rem, count));
2956
temp /= 10;
3057
}
3158

32-
if (sum == n) {
33-
cout << n << " is an armstrong number";
59+
if (result == number) {
60+
return true;
3461
} else {
35-
cout << n << " is not an armstrong number";
62+
return false;
3663
}
64+
}
65+
} // namespace dynamic_programming
3766

67+
/**
68+
* @brief Self-test implementations
69+
* @returns void
70+
*/
71+
static void tests() {
72+
assert(dynamic_programming::is_armstrong(153) == true);
73+
assert(dynamic_programming::is_armstrong(1) == true);
74+
assert(dynamic_programming::is_armstrong(0) == true);
75+
assert(dynamic_programming::is_armstrong(370) == true);
76+
assert(dynamic_programming::is_armstrong(1634) == true);
77+
assert(dynamic_programming::is_armstrong(580) == false);
78+
assert(dynamic_programming::is_armstrong(15) == false);
79+
assert(dynamic_programming::is_armstrong(1024) == false);
80+
assert(dynamic_programming::is_armstrong(989) == false);
81+
assert(dynamic_programming::is_armstrong(103) == false);
82+
83+
std::cout << "All tests have successfully passed!\n";
84+
}
85+
86+
/**
87+
* @brief Main function
88+
* @returns 0 on exit
89+
*/
90+
int main() {
91+
tests(); // run self-test implementations
3892
return 0;
3993
}

0 commit comments

Comments
 (0)