Foundations Working with digits and divisors
31. Armstrong Number
Given a positive integer n, return true if n is an Armstrong number, and false otherwise.
Let k be the number of digits in n. Raise every digit of n to the power k, and add up the results.
n is an Armstrong number when that sum equals n itself.
For example, 407 has 3 digits, and 43 + 03 + 73 = 64 + 0 + 343 = 407. So 407 is an Armstrong number.
Example 1
Input: n = 1634
trueExplanation: 1634 has 4 digits, and 14 + 64 + 34 + 44 = 1 + 1296 + 81 + 256 = 1634.
Example 2
Input: n = 154
falseExplanation: 154 has 3 digits, and 13 + 53 + 43 = 1 + 125 + 64 = 190, which is not 154.
Example 3
Input: n = 4679307774
trueExplanation: Its ten digits, each raised to the power 10, add up to 4,679,307,774.
Check your understanding
Work out the answer for this input before you write any code.
Constraints
1 ≤ n ≤ 9 × 1015
Clarifying questions
What you would ask an interviewer before writing any code.