1
1
/* *
2
- * @file
3
- * \brief Program to check if a number is an [Armstrong/Narcissistic
4
- * number](https://en.wikipedia.org/wiki/Narcissistic_number) in decimal system.
5
- *
6
- * \details
7
- * Armstrong number or [Narcissistic
8
- * number](https://en.wikipedia.org/wiki/Narcissistic_number) is a number that
9
- * is the sum of its own digits raised to the power of the number of digits.
10
- * @author iamnambiar
2
+ * @file
3
+ * @brief Program to check if a number is an [Armstrong/Narcissistic
4
+ * number](https://en.wikipedia.org/wiki/Narcissistic_number) in decimal system.
5
+ *
6
+ * @details
7
+ * Armstrong number or [Narcissistic
8
+ * number](https://en.wikipedia.org/wiki/Narcissistic_number) is a number that
9
+ * is the sum of its own digits raised to the power of the number of digits.
10
+ *
11
+ * let n be the narcissistic number,
12
+ * \f[F_b(n) = \sum_{i=0}^{k-1}d_{i}^{k}\f] for
13
+ * \f$ b > 1 F_b : \N \to \N \f$ where
14
+ * \f$ k = \lfloor log_b n\rfloor is the number of digits in the number in base \f$b\f$, and
15
+ * \f$ d_i = \frac{n mod b^{i+1} - n mod b^{i}}{b^{i}} \f$
16
+ *
17
+ * @author [Neeraj Cherkara](https://github.com/iamnambiar)
11
18
*/
12
- #include < cassert>
13
- #include < cmath>
14
- #include < iostream>
19
+ #include < cassert> // / for assert
20
+ #include < cmath> // / for std::pow
15
21
16
22
/* *
17
- * Function to calculate the total number of digits in the number.
18
- * @param num Number
19
- * @return Total number of digits.
23
+ * @brief Function to calculate the total number of digits in the number.
24
+ * @param num Number
25
+ * @return Total number of digits.
20
26
*/
21
27
int number_of_digits (int num) {
22
28
int total_digits = 0 ;
@@ -28,16 +34,17 @@ int number_of_digits(int num) {
28
34
}
29
35
30
36
/* *
31
- * Function to check whether the number is armstrong number or not.
32
- * @param num Number
33
- * @return `true` if the number is armstrong.
34
- * @return `false` if the number is not armstrong.
37
+ * @brief Function to check whether the number is armstrong number or not.
38
+ * @param number to be checked
39
+ * @return `true` if the number is armstrong.
40
+ * @return `false` if the number is not armstrong.
35
41
*/
36
42
bool is_armstrong (int number) {
37
- // If the number is less than 0, then it is not a armstrong number.
43
+ // If the number is less than 0, then it is not an armstrong number.
38
44
if (number < 0 ) {
39
45
return false ;
40
46
}
47
+
41
48
int sum = 0 ;
42
49
int temp = number;
43
50
// Finding the total number of digits in the number
@@ -46,17 +53,17 @@ bool is_armstrong(int number) {
46
53
int rem = temp % 10 ;
47
54
// Finding each digit raised to the power total digit and add it to the
48
55
// total sum
49
- sum = sum + std::pow (rem, total_digits);
56
+ sum += static_cast < int >( std::pow (rem, total_digits) );
50
57
temp = temp / 10 ;
51
58
}
52
59
return number == sum;
53
60
}
54
61
55
62
/* *
56
- * Function for testing the is_armstrong() function
57
- * with all the test cases.
63
+ * @brief Self-test implementations
64
+ * @returns void
58
65
*/
59
- void test () {
66
+ static void test () {
60
67
// is_armstrong(370) returns true.
61
68
assert (is_armstrong (370 ) == true );
62
69
// is_armstrong(225) returns false.
@@ -72,9 +79,10 @@ void test() {
72
79
}
73
80
74
81
/* *
75
- * Main Function
82
+ * @brief Main Function
83
+ * @returns 0 on exit
76
84
*/
77
85
int main () {
78
- test ();
86
+ test (); // run self-test implementations
79
87
return 0 ;
80
88
}
0 commit comments