Prerequisites

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

  1. Convert n to a string and get its length k.
  2. Extract each digit by repeatedly taking n % 10 and dividing by 10.
  3. For each digit, add digit^k to a running sum.
  4. Return true if the sum equals the original number.
class Solution:
    def isArmstrong(self, n: int) -> bool:
        def getSumOfKthPowerOfDigits(num, k):
            result = 0

            while num != 0:
                result += (num % 10) ** k
                num //= 10

            return result
        
        length = len(str(n))
        
        return getSumOfKthPowerOfDigits(n, length) == n

Time & Space Complexity

  • Time complexity: O(M)O(M)

  • Space complexity: O(1)O(1) constant space

Where MM 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

  1. Compute k = floor(log10(n)) + 1 to get the digit count.
  2. Extract each digit using modulo and division.
  3. Sum up each digit raised to the power of k.
  4. Return true if the sum equals n.
class Solution:
    def isArmstrong(self, n: int) -> bool:
        def getSumOfKthPowerOfDigits(num, k):
            result = 0

            while num != 0:
                result += (num % 10) ** k
                num //= 10

            return result
        
        length = int(math.log10(n)) + 1
        
        return getSumOfKthPowerOfDigits(n, length) == n

Time & Space Complexity

  • Time complexity: O(M)O(M)

  • Space complexity: O(1)O(1) constant space

Where MM 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

  1. Copy n to a temporary variable.
  2. Count digits by dividing by 10 until the number becomes 0.
  3. Extract each digit from the original number and sum digit^k.
  4. Return true if the sum equals n.
class Solution:
    def isArmstrong(self, n: int) -> bool:
        def getSumOfKthPowerOfDigits(num, k):
            result = 0

            while num != 0:
                result += (num % 10) ** k
                num //= 10
                
            return result

        length = 0
        temp_n = n

        while temp_n != 0:
            length += 1
            temp_n //= 10
        
        return getSumOfKthPowerOfDigits(n, length) == n

Time & Space Complexity

  • Time complexity: O(M)O(M)

  • Space complexity: O(1)O(1) constant space

Where MM 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 later
while n != 0:
    result += (n % 10) ** k
    n //= 10
return result == n  # n is now 0!