Talentd
FleetCode

1134. Armstrong Number

Easy77.8% acceptance1 companyPremium elsewhere, free here

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 1:

Input: n = 153
Output: true
Explanation: 153 is a 3-digit number, and 153 = 13 + 53 + 33.

Example 2:

Input: n = 123
Output: false
Explanation: 123 is a 3-digit number, and 123 != 13 + 23 + 33 = 36.

Constraints:

  • 1 <= n <= 108

Asked at 1 company

Topics

Hints open one at a time

Hint 1
Check if the given k-digit number equals the sum of the k-th power of it's digits.
Hint 2
How to compute the sum of the k-th power of the digits of a number ? Can you divide the number into digits using division and modulus operations ?
Hint 3
You can find the least significant digit of a number by taking it modulus 10. And you can remove it by dividing the number by 10 (integer division). Once you have a digit, you can raise it to the power of k and add it to the sum.
8:52
Still stuck?Read the written solution, or watch a walkthrough.
Open solution
Loading editor…
1class Solution:
2 def solve(self):
3 pass
4
Starter codePython 3Spaces: 4
Examples
Input
153
Input
123
Your code is saved on this device.Solution