Armstrong Number
Pattern: Maths basics
Check whether a number equals the sum of its digits, each raised to the power of how many digits it has.
The problem
You are given a whole number n. Let k be the number of digits in n. Return true if raising each digit to the
power k and adding the results gives back n, and false otherwise. For example, 153 has 3 digits and
1³ + 5³ + 3³ = 1 + 125 + 27 = 153, so it is an Armstrong number. 123 is not, because 1 + 8 + 27 = 36.
On LeetCode this is a premium problem, and the function name isArmstrong matches it. This guide accepts any n from
0 up to the 32-bit limit.
Think first
- Try 9474. How many digits does it have, so what power do you use? Is it an Armstrong number?
- You need the digit count before you can raise anything. How many passes over the digits does that suggest?
- If the digit loop shrinks
nto 0, how will you compare the sum with the original number at the end?
Hints
Brute force, and why it fails
Turning the number into text makes this short: the text's length is the digit count, and each character gives one
digit. That is correct and fast for numbers this size. It hides the arithmetic, though, and it is easy to reach for
pow and run into rounding. The version here uses the two moves this pattern is built on, % 10 and / 10, and only
whole numbers.
The key insight
Two separate jobs, two passes: count the digits first, then add up digit^count. Working on a copy of n keeps the
original for the final comparison.
Dry run
| n | 153 |
|---|---|
| digits | 3 |
| sum | 0 |
n = 153. First pass, count the digits. 153 has 3 digits, so every digit will be raised to the power 3.
The plan
digits = 0
rest = n
while rest > 0: # first pass: count the digits
digits = digits + 1
rest = rest div 10
sum = 0
rest = n
while rest > 0: # second pass: add each digit to the power `digits`
power = 1
repeat digits times:
power = power * (rest mod 10)
sum = sum + power
rest = rest div 10
return sum == n
Complexity
- Time
O(d²)- Space
O(1)
d is the number of digits, at most 10. Each of the d digits is raised with a loop of d multiplications. That is at
most 100 small steps, so it is effectively instant.
Edge cases
- Every single-digit number (including 0) is an Armstrong number, because a digit to the power 1 is itself.
- 10 is not: 1¹ + 0¹ = 1.
- 9474 shows why the power must be the digit count, not always 3.
- Near the 32-bit limit the sum can pass 2³¹. For 1,999,999,999 it is 1 + 9 × 9¹⁰, over 31 billion. Keep the sum in a 64-bit type.
I've built my logic, show the code
Java
class Solution {
public boolean isArmstrong(int n) {
int digits = 0;
for (int rest = n; rest > 0; rest = rest / 10) {
digits++;
}
long sum = 0;
for (int rest = n; rest > 0; rest = rest / 10) {
long power = 1;
for (int i = 0; i < digits; i++) {
power = power * (rest % 10);
}
sum = sum + power;
}
return sum == n;
}
}C++
class Solution {
public:
bool isArmstrong(int n) {
int digits = 0;
for (int rest = n; rest > 0; rest = rest / 10) {
digits++;
}
long long sum = 0;
for (int rest = n; rest > 0; rest = rest / 10) {
long long power = 1;
for (int i = 0; i < digits; i++) {
power = power * (rest % 10);
}
sum = sum + power;
}
return sum == n;
}
};Python
class Solution:
def isArmstrong(self, n: int) -> bool:
digits = 0
rest = n
while rest > 0:
digits += 1
rest = rest // 10
total = 0
rest = n
while rest > 0:
power = 1
for _ in range(digits):
power = power * (rest % 10)
total = total + power
rest = rest // 10
return total == nCommon mistakes
- Always cubing the digits, which only works for three-digit numbers.
- Peeling digits from
nitself and then comparing the sum with the now-zeron. - Using floating-point
powand getting off-by-one results after rounding down. - Keeping the sum in a 32-bit int for large inputs.