Before attempting this problem, you should be comfortable with:
Digit Extraction - Using modulo and division to extract individual digits from a number
Exponentiation - Raising numbers to a power
Basic Math - Understanding number properties and digit counting techniques
1. Calculate k by Converting to String
Intuition
An Armstrong number equals the sum of its digits each raised to the power of the total digit count. For example, 153 has 3 digits, and 1^3 + 5^3 + 3^3 = 153.
The straightforward approach is to convert the number to a string to count digits, then extract each digit and compute the sum of powers. We compare this sum to the original number.
Algorithm
Convert n to a string and get its length k.
Extract each digit by repeatedly taking n % 10 and dividing by 10.
For each digit, add digit^k to a running sum.
Return true if the sum equals the original number.
classSolution:defisArmstrong(self, n:int)->bool:defgetSumOfKthPowerOfDigits(num, k):
result =0while num !=0:
result +=(num %10)** k
num //=10return result
length =len(str(n))return getSumOfKthPowerOfDigits(n, length)== n
classSolution{publicintgetSumOfKthPowerOfDigits(int n,int k){int result =0;while(n !=0){
result +=Math.pow(n %10, k);
n /=10;}return result;}publicbooleanisArmstrong(int n){int length =String.valueOf(n).length();returngetSumOfKthPowerOfDigits(n, length)== n;}}
classSolution{public:intgetSumOfKthPowerOfDigits(int n,int k){int result =0;while(n !=0){
result +=pow(n %10, k);
n /=10;}return result;}boolisArmstrong(int n){int length =to_string(n).length();returngetSumOfKthPowerOfDigits(n, length)== n;}};
classSolution{/**
* @param {number} n
* @return {boolean}
*/isArmstrong(n){constgetSumOfKthPowerOfDigits=(num, k)=>{let result =0;while(num !==0){
result += Math.pow(num %10, k);
num = Math.floor(num /10);}return result;};const length =String(n).length;returngetSumOfKthPowerOfDigits(n, length)=== n;}}
publicclassSolution{privateintGetSumOfKthPowerOfDigits(int n,int k){int result =0;while(n !=0){
result +=(int)Math.Pow(n %10, k);
n /=10;}return result;}publicboolIsArmstrong(int n){int length = n.ToString().Length;returnGetSumOfKthPowerOfDigits(n, length)== n;}}
funcisArmstrong(n int)bool{
getSumOfKthPowerOfDigits :=func(num, k int)int{
result :=0for num !=0{
digit := num %10
power :=1for i :=0; i < k; i++{
power *= digit
}
result += power
num /=10}return result
}
length :=len(fmt.Sprintf("%d", n))returngetSumOfKthPowerOfDigits(n, length)== n
}
class Solution {privatefungetSumOfKthPowerOfDigits(n: Int, k: Int): Int {var num = n
var result =0while(num !=0){
result += Math.pow((num %10).toDouble(), k.toDouble()).toInt()
num /=10}return result
}funisArmstrong(n: Int): Boolean {val length = n.toString().length
returngetSumOfKthPowerOfDigits(n, length)== n
}}
classSolution{funcisArmstrong(_ n:Int)->Bool{funcgetSumOfKthPowerOfDigits(_ num:Int,_ k:Int)->Int{var num = num
var result =0while num !=0{var power =1for_in0..<k {
power *= num %10}
result += power
num /=10}return result
}let length =String(n).count
returngetSumOfKthPowerOfDigits(n, length)== n
}}
Time & Space Complexity
Time complexity: O(M)
Space complexity: O(1) constant space
Where M is the number of digits in the input integer n.
2. Calculate k by Using Log
Intuition
Instead of converting to a string, we can use logarithms to count digits. The number of digits in a positive integer n is floor(log10(n)) + 1. This avoids string allocation and can be slightly more efficient.
The rest of the logic remains the same: extract digits, raise each to the power of k, sum them up, and compare.
Algorithm
Compute k = floor(log10(n)) + 1 to get the digit count.
classSolution:defisArmstrong(self, n:int)->bool:defgetSumOfKthPowerOfDigits(num, k):
result =0while num !=0:
result +=(num %10)** k
num //=10return result
length =int(math.log10(n))+1return getSumOfKthPowerOfDigits(n, length)== n
classSolution{publicintgetSumOfKthPowerOfDigits(int n,int k){int result =0;while(n !=0){
result +=Math.pow(n %10, k);
n /=10;}return result;}publicbooleanisArmstrong(int n){int length =(int)Math.log10(n)+1;returngetSumOfKthPowerOfDigits(n, length)== n;}}
classSolution{public:intgetSumOfKthPowerOfDigits(int n,int k){int result =0;while(n !=0){
result +=pow(n %10, k);
n /=10;}return result;}boolisArmstrong(int n){int length =log10(n)+1;returngetSumOfKthPowerOfDigits(n, length)== n;}};
classSolution{/**
* @param {number} n
* @return {boolean}
*/isArmstrong(n){constgetSumOfKthPowerOfDigits=(num, k)=>{let result =0;while(num !==0){
result += Math.pow(num %10, k);
num = Math.floor(num /10);}return result;};const length = Math.floor(Math.log10(n))+1;returngetSumOfKthPowerOfDigits(n, length)=== n;}}
publicclassSolution{privateintGetSumOfKthPowerOfDigits(int n,int k){int result =0;while(n !=0){
result +=(int)Math.Pow(n %10, k);
n /=10;}return result;}publicboolIsArmstrong(int n){int length =(int)Math.Log10(n)+1;returnGetSumOfKthPowerOfDigits(n, length)== n;}}
funcisArmstrong(n int)bool{
getSumOfKthPowerOfDigits :=func(num, k int)int{
result :=0for num !=0{
digit := num %10
power :=1for i :=0; i < k; i++{
power *= digit
}
result += power
num /=10}return result
}
length :=int(math.Log10(float64(n)))+1returngetSumOfKthPowerOfDigits(n, length)== n
}
class Solution {privatefungetSumOfKthPowerOfDigits(n: Int, k: Int): Int {var num = n
var result =0while(num !=0){
result += Math.pow((num %10).toDouble(), k.toDouble()).toInt()
num /=10}return result
}funisArmstrong(n: Int): Boolean {val length =(Math.log10(n.toDouble())+1).toInt()returngetSumOfKthPowerOfDigits(n, length)== n
}}
classSolution{funcisArmstrong(_ n:Int)->Bool{funcgetSumOfKthPowerOfDigits(_ num:Int,_ k:Int)->Int{var num = num
var result =0while num !=0{var power =1for_in0..<k {
power *= num %10}
result += power
num /=10}return result
}let length =Int(log10(Double(n)))+1returngetSumOfKthPowerOfDigits(n, length)== n
}}
Time & Space Complexity
Time complexity: O(M)
Space complexity: O(1) constant space
Where M is the number of digits in the input integer n.
3. Calculate k Without Built-in Methods
Intuition
We can count digits without any built-in functions by simply dividing the number by 10 until it becomes 0, counting iterations. This approach uses only basic arithmetic and works in any language without library dependencies.
Algorithm
Copy n to a temporary variable.
Count digits by dividing by 10 until the number becomes 0.
Extract each digit from the original number and sum digit^k.
classSolution:defisArmstrong(self, n:int)->bool:defgetSumOfKthPowerOfDigits(num, k):
result =0while num !=0:
result +=(num %10)** k
num //=10return result
length =0
temp_n = n
while temp_n !=0:
length +=1
temp_n //=10return getSumOfKthPowerOfDigits(n, length)== n
classSolution{publicintgetSumOfKthPowerOfDigits(int n,int k){int result =0;while(n !=0){
result +=Math.pow(n %10, k);
n /=10;}return result;}publicbooleanisArmstrong(int n){int length =0;int tempN = n;while(tempN !=0){
length++;
tempN /=10;}returngetSumOfKthPowerOfDigits(n, length)== n;}}
classSolution{public:intgetSumOfKthPowerOfDigits(int n,int k){int result =0;while(n !=0){
result +=pow(n %10, k);
n /=10;}return result;}boolisArmstrong(int n){int length =0;int tempN = n;while(tempN){
length++;
tempN /=10;}returngetSumOfKthPowerOfDigits(n, length)== n;}};
classSolution{/**
* @param {number} n
* @return {boolean}
*/isArmstrong(n){constgetSumOfKthPowerOfDigits=(num, k)=>{let result =0;while(num !==0){
result += Math.pow(num %10, k);
num = Math.floor(num /10);}return result;};let length =0;let tempN = n;while(tempN !==0){
length++;
tempN = Math.floor(tempN /10);}returngetSumOfKthPowerOfDigits(n, length)=== n;}}
publicclassSolution{privateintGetSumOfKthPowerOfDigits(int n,int k){int result =0;while(n !=0){
result +=(int)Math.Pow(n %10, k);
n /=10;}return result;}publicboolIsArmstrong(int n){int length =0;int tempN = n;while(tempN !=0){
length++;
tempN /=10;}returnGetSumOfKthPowerOfDigits(n, length)== n;}}
funcisArmstrong(n int)bool{
getSumOfKthPowerOfDigits :=func(num, k int)int{
result :=0for num !=0{
digit := num %10
power :=1for i :=0; i < k; i++{
power *= digit
}
result += power
num /=10}return result
}
length :=0
tempN := n
for tempN !=0{
length++
tempN /=10}returngetSumOfKthPowerOfDigits(n, length)== n
}
class Solution {privatefungetSumOfKthPowerOfDigits(n: Int, k: Int): Int {var num = n
var result =0while(num !=0){
result += Math.pow((num %10).toDouble(), k.toDouble()).toInt()
num /=10}return result
}funisArmstrong(n: Int): Boolean {var length =0var tempN = n
while(tempN !=0){
length++
tempN /=10}returngetSumOfKthPowerOfDigits(n, length)== n
}}
classSolution{funcisArmstrong(_ n:Int)->Bool{funcgetSumOfKthPowerOfDigits(_ num:Int,_ k:Int)->Int{var num = num
var result =0while num !=0{var power =1for_in0..<k {
power *= num %10}
result += power
num /=10}return result
}var length =0var tempN = n
while tempN !=0{
length +=1
tempN /=10}returngetSumOfKthPowerOfDigits(n, length)== n
}}
Time & Space Complexity
Time complexity: O(M)
Space complexity: O(1) constant space
Where M is the number of digits in the input integer n.
Common Pitfalls
Using a Fixed Power Instead of the Digit Count
A common mistake is hardcoding the exponent (e.g., always using 3) instead of dynamically calculating the number of digits. Armstrong numbers are defined with respect to their digit count, so 153 uses power 3, but 1634 uses power 4.
# Wrong: always using power 3
result += digit **3
Modifying the Original Number Without Saving It
When extracting digits by repeatedly dividing by 10, the original number gets destroyed. Forgetting to save the original value before the loop means you have nothing to compare the sum against.
# Wrong: n is modified and can't be compared laterwhile n !=0:
result +=(n %10)** k
n //=10return result == n # n is now 0!