Leetcode 1134: Armstrong Number

Determining if a number is an Armstrong number by extracting its digits.

The original problem is here.

Armstrong Number

My solution

class Solution:
    def isArmstrong(self, n: int) -> int:
        # Convert integer to string to easily count and iterate over digits
        n_str = []
        while n > 0:
            n_str.append(n % 10)
            n //= 10
        
        k = len(n_str)
        
        # Generator expression to calculate the sum of the digits raised to the power of k
        total_sum = sum(int(digit) ** k for digit in n_str)
        
        # Check if the calculated sum matches the original number
        return total_sum == n

Complexity Analysis

Follow-up question: factorion

class Solution:
    def isFactorion(self, n: int) -> bool:
        # Convert integer to string to easily count and iterate over digits
        n_str = []
        while n > 0:
            n_str.append(n % 10)
            n //= 10
        
        k = len(n_str)
        
        if 9! * k < 10**(k-1):
            return False
        # Generator expression to calculate the sum of the digits raised to the power of k
        total_sum = sum(int(digit) ** k for digit in n_str)
        
        # Check if the calculated sum matches the original number
        return total_sum == n