Foundations Working with digits and divisors

31. Armstrong Number

Easy

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

Output: true

Explanation: 1634 has 4 digits, and 14 + 64 + 34 + 44 = 1 + 1296 + 81 + 256 = 1634.

Example 2

Input: n = 154

Output: false

Explanation: 154 has 3 digits, and 13 + 53 + 43 = 1 + 125 + 64 = 190, which is not 154.

Example 3

Input: n = 4679307774

Output: true

Explanation: 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.

n = 8208

Constraints

  • 1 ≤ n ≤ 9 × 1015

Clarifying questions

What you would ask an interviewer before writing any code.

Hints

Interview follow-ups

Runs in your browser
Code
Runs in your browser for now · some things may not work as expectedLn 1, Col 1

Sign in to oneby7

Keep your progress, your streak and your code, on any device.

New here? This creates your account too.