Skip to content

Commit 45a7b79

Browse files
committed
😉feat: addded docs for armstrong number
1 parent bbfce50 commit 45a7b79

File tree

1 file changed

+105
-0
lines changed

1 file changed

+105
-0
lines changed

Diff for: en/Basic Math/Armstrong_Number.md

+105
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
1+
# Armstrong Number
2+
3+
An Armstrong number (also known as a narcissistic number) is a number that is equal to the sum of its own digits each raised to the power of the number of digits. For example, the number 153 is an Armstrong number because it has 3 digits, and if we calculate $(1^3 + 5^3 + 3^3)$, we get $(1 + 125 + 27 = 153)$. In mathematical terms, a number $(n)$ with $(d)$ digits is an Armstrong number if:
4+
5+
$$ n = \sum_{i=1}^{d} (digit_i)^d $$
6+
7+
## Facts about Armstrong numbers
8+
9+
1. The smallest Armstrong number is 0, and the largest 3-digit Armstrong number is 9474.
10+
2. All single-digit numbers (0-9) are considered Armstrong numbers since they are equal to themselves raised to the power of 1.
11+
3. Armstrong numbers can exist in any base, not just base 10; for example, 1, 2, and 3 are Armstrong numbers in base 2.
12+
4. The number of Armstrong numbers is finite; for instance, there are only 88 Armstrong numbers in base 10.
13+
5. The largest known Armstrong number is 2, and it has 39 digits.
14+
15+
# Armstrong Number
16+
17+
An Armstrong number (also known as a narcissistic number, pluperfect number, or pluperfect digital invariant) in a given number base $b$ is a number that is the sum of its own digits each raised to the power of the number of digits. In decimal (base-10), an Armstrong number of three digits is an integer such that the sum of the cubes of its digits is equal to the number itself. For example, $153 = 1^3 + 5^3 + 3^3$.
18+
19+
## Facts About Armstrong Numbers
20+
21+
1. The smallest Armstrong number in base-10 is $0$.
22+
2. Every single-digit number is an Armstrong number because any number raised to the power of one is the number itself.
23+
3. The Armstrong numbers between $100$ and $999$ are $153$, $370$, $371$, and $407$.
24+
4. The concept can be extended to other bases, not just base-10.
25+
5. Larger Armstrong numbers exist, but they are rare and grow quickly in size as the number of digits increases.
26+
27+
## Approach For Checking Armstrong Number
28+
29+
### Step 1: *Counting the digits*
30+
Determine the number of digits in the number. This step is crucial because each digit will be raised to the power of the total number of digits. For example, for the number $153$, there are $3$ digits. This count will be the exponent in our calculations.
31+
32+
- **Time Complexity**: $O(\log_{10} N)$
33+
- **Reason**: The number of digits in $N$ is proportional to $\log_{10} N$.
34+
35+
### Step 2: *Extracting digits*
36+
Extract each digit of the number. We need to handle each digit individually to raise it to the power determined in the first step. This can be done by converting the number to a string or using mathematical operations. For instance, for $153$, we need to handle $1$, $5$, and $3$ separately.
37+
38+
- **Time Complexity**: $O(\log_{10} N)$
39+
- **Reason**: You need to process each digit, and there are $\log_{10} N$ digits.
40+
41+
### Step 3: *Calculating the power sum*
42+
Raise each extracted digit to the power determined in Step 1 and calculate the sum of these powered digits. This is the core calculation where we see if the sum of the powered digits matches the original number. For $153$, this involves computing $1^3 + 5^3 + 3^3 = 1 + 125 + 27 = 153$.
43+
44+
- **Time Complexity**: $O((\log_{10} N)^2)$
45+
- **Reason**: Calculating the power of a single digit takes $O(d)$ time, where $d$ is the number of digits (which is $\log_{10} N$). Thus, for all digits, it takes $O(\log_{10} N \cdot \log_{10} N) = O((\log_{10} N)^2)$.
46+
47+
### Step 4: *Comparing sums*
48+
Finally, compare the calculated sum to the original number. If the sum equals the original number, then the number is an Armstrong number. Otherwise, it is not. For $153$, since $153 = 153$, it confirms that $153$ is indeed an Armstrong number.
49+
50+
- **Time Complexity**: $O(1)$
51+
- **Reason**: Simply comparing two integers.
52+
53+
### Final Time Complexity
54+
The overall time complexity of the algorithm is dominated by the power sum calculation step:
55+
56+
$$O((\log N)^2)$$
57+
58+
59+
60+
This represents the quadratic nature of the operations concerning the number of digits in the number $N$.
61+
62+
## Pseudo Code
63+
64+
```cpp
65+
bool isArmstrong(int n) {
66+
// Step 1: Determine the number of digits by counting the number of digits
67+
int d = 0;
68+
int temp = n;
69+
70+
while (temp > 0) {
71+
temp = temp / 10;
72+
d = d + 1;
73+
}
74+
75+
// Step 2: Initialize sum variable
76+
int armstrongSum = 0;
77+
// Reset temp to the original number after being done
78+
temp = n;
79+
80+
81+
// Step 3: Extract digits and calculate the sum of powers
82+
while (temp > 0) {
83+
// Get the last digit
84+
int digit = temp % 10;
85+
// Raise the digit to the power of d and add to the sum
86+
armstrongSum = armstrongSum + (digit ^ d);
87+
// Remove the last digit from temp
88+
temp = temp / 10;
89+
}
90+
91+
// Step 4: Compare the sum with the original number
92+
if (armstrongSum == n)
93+
return true; // n is an Armstrong number
94+
else
95+
return false; // n isn't an Armstrong number
96+
}
97+
```
98+
99+
## Implementations
100+
- [TheAlgorithms](https://the-algorithms.com/algorithm/aliquot-sum)
101+
102+
## Sources
103+
- [Wikipedia](https://www.wikiwand.com/en/articles/Narcissistic_number)
104+
- [Azim Premji University](https://publications.azimpremjiuniversity.edu.in/3157/1/07_satvik_notearmstrongnumbers.pdf) (PDF)
105+
- [GeeksForGeeks](https://www.geeksforgeeks.org/program-for-armstrong-numbers/)

0 commit comments

Comments
 (0)