Armstrong Number

Easy
166.

Armstrong Number

Armstrong Number is an easy coding problem on math, asked in TCS, Infosys, Wipro and Accenture interviews.

A number is an Armstrong number (also called a narcissistic number) when the sum of its own digits, each raised to the power of the number of digits, equals the number itself.

Given a positive integer n, return true if n is an Armstrong number.

Example 1

Input
n = 153
Output
true
Explanation
It has 3 digits and 1³ + 5³ + 3³ = 1 + 125 + 27 = 153.

Example 2

Input
n = 123
Output
false
Explanation
1³ + 2³ + 3³ = 36, not 123.

Example 3

Input
n = 9474
Output
true
Explanation
9⁴ + 4⁴ + 7⁴ + 4⁴ = 9474.

Constraints

  • 1 <= n <= 1000000
Write your solution here. Running it against the tests needs a free account — what you type comes with you.Sign in to run
/**
 * @param {number} n
 * @return {boolean}
 */
var isArmstrong = function(n) {
    // Write your code here
    
};

Sign in to run your own test cases

Sign in

Armstrong Number — Solution in Python, Java & C++

Armstrong Number solution explained: step-by-step approach, complexity and code in Python, Java, C++ and JavaScript. An easy problem on math.

Armstrong Number is an easy coding problem on math, asked in TCS, Infosys, Wipro and Accenture interviews.

Problem statement

A number is an Armstrong number (also called a narcissistic number) when the sum of its own digits, each raised to the power of the number of digits, equals the number itself.

Given a positive integer n, return true if n is an Armstrong number.

Example 1

Input: n = 153
Output: true
Explanation: It has 3 digits and 1³ + 5³ + 3³ = 1 + 125 + 27 = 153.

Example 2

Input: n = 123
Output: false
Explanation: 1³ + 2³ + 3³ = 36, not 123.

Example 3

Input: n = 9474
Output: true
Explanation: 9⁴ + 4⁴ + 7⁴ + 4⁴ = 9474.

Constraints

  • 1 <= n <= 1000000

Hints for Armstrong Number

  1. Hint 1

    Count the digits first — that count is the exponent every digit is raised to.

  2. Hint 2

    Then sum digit ^ k over the digits and compare with the original number.

  3. Hint 3

    Single-digit numbers are all Armstrong numbers, since d¹ = d.

How to solve Armstrong Number

Count the digits to fix the exponent, then compare the sum of each digit raised to that power with the number.

Approach

  1. Let k be the number of digits of n.
  2. Sum d^k over every digit d.
  3. Return whether the sum equals n.

Why it works

That is the definition; the only subtlety is that the exponent is the total digit count and must be computed before the summation begins.

Complexity

  • Time — O(k)
  • Space — O(1)

Pitfalls

  • Using a fixed exponent of 3 only tests the three-digit case — 9474 = 9⁴ + 4⁴ + 7⁴ + 4⁴ is Armstrong too.
  • Every single-digit number qualifies, since d¹ = d.

Reference solution

Python

def isArmstrong(n: int) -> bool:
    s = str(n)
    k = len(s)
    return sum(int(ch) ** k for ch in s) == n

Java

public static boolean isArmstrong(int n) {
    String s = String.valueOf(n);
    int k = s.length();
    long total = 0;
    for (int i = 0; i < k; i++) {
        long d = s.charAt(i) - '0';
        long p = 1;
        for (int e = 0; e < k; e++) p *= d;
        total += p;
    }
    return total == n;
}

C++

bool isArmstrong(int n) {
    string s = to_string(n);
    int k = (int) s.size();
    long long total = 0;
    for (int i = 0; i < k; i++) {
        long long d = s[i] - '0';
        long long p = 1;
        for (int e = 0; e < k; e++) p *= d;
        total += p;
    }
    return total == (long long) n;
}

JavaScript

var isArmstrong = function(n) {
    const s = String(n);
    const k = s.length;
    let total = 0;
    for (let i = 0; i < k; i++) {
        total += Math.pow(s.charCodeAt(i) - 48, k);
    }
    return total === n;
};

Also on the editorial tab: C, C#, Go, Kotlin, PHP, Ruby, Rust, Swift, TypeScript.

All 337 math problems · the whole catalogue