Armstrong Number — LeetCode 1134 Python Solution
- Problem
- #1134
- Pattern
- Math and Number Theory
- Reading time
- 2 min
- Source
- leetcode.com
The problem
Given an integer 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 kth power of each digit sums to n.
Example
- Input
- n = 153
- Output
- true
- Explanation
- 153 is a 3-digit number, and 153 = 13 + 53 + 33.
Python solution
class Solution:
def isArmstrong(self, n: int) -> bool:
k = len(str(n))
s, x = 0, n
while x:
s += (x % 10) ** k
x //= 10
return s == nComplexity
| Measure | Complexity |
|---|---|
| Time | O(\log n) |
| Space | O(\log n) auxiliary |
Pattern: Math and Number Theory
Find the closed form, the invariant, or the modular identity — and skip the loop entirely. LeetCode 1134. Armstrong Number is filed here on both counts: the reference solution below belongs to the algorithm family this hub collects, and LeetCode tags it Math.
The math and number theory guide has the Python template for the pattern and the 485 LeetCode problems that use it.
Related problems
Frequently asked questions
- How hard is LeetCode 1134. Armstrong Number?
- LeetCode 1134. Armstrong Number is rated Easy on LeetCode.
- What is the time complexity of LeetCode 1134. Armstrong Number?
- The Python solution on this page runs in O(\log n).
- What is the space complexity of LeetCode 1134. Armstrong Number?
- The Python solution on this page uses O(\log n) auxiliary space.
- What topics does LeetCode 1134. Armstrong Number cover?
- LeetCode 1134. Armstrong Number is tagged Math on LeetCode.
- Is LeetCode 1134. Armstrong Number a premium problem?
- Yes. LeetCode 1134. Armstrong Number is a LeetCode Premium problem, so the full statement and test cases require a paid LeetCode subscription.