Problem
Given an integern, return true if and only if it is an Armstrong number.
The k-digit number n is an Armstrong number if and only if the k^th power of each digit sums to n.
Examples
Constraints
- 1 <= n <= 10^8
Documentation Index
Fetch the complete documentation index at: /llms.txt
Use this file to discover all available pages before exploring further.
Tested Python solution for LeetCode 1134 with 32 pytest cases. Generate a practice environment with lcpy.
lcpy gen -n 1134 # by problem number
lcpy gen -s armstrong_number # by problem name
n, return true if and only if it is an Armstrong number.
The k-digit number n is an Armstrong number if and only if the k^th power of each digit sums to n.
Input: n = 153
Output: true
Explanation: 153 is a 3-digit number, and 153 = 1^3 + 5^3 + 3^3.
Input: n = 123
Output: false
Explanation: 123 is a 3-digit number, and 123 != 1^3 + 2^3 + 3^3 = 36.
class Solution:
# Time: O(log n)
# Space: O(1)
def is_armstrong(self, n: int) -> bool:
digits = str(n)
k = len(digits)
return sum(int(d) ** k for d in digits) == n
| Time | Space |
|---|---|
| O(log n) | O(1) |