Check whether a number equals the sum of its digits each raised to the digit count

Quick Overview

Decide whether a positive integer is an Armstrong (narcissistic) number, meaning it equals the sum of its digits each raised to the power of the digit count. It tests digit extraction with division and modulo, counting digits, and comparing against the untouched original value.

Check whether a number equals the sum of its digits each raised to the digit count

Company: Tech Mahindra

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: easy

Interview Round: Other

Given a positive integer `n`, return `True` if `n` is an Armstrong number (also called a narcissistic number), and `False` otherwise. An Armstrong number equals the sum of its digits, each raised to the power of the number of digits it has. For example, 153 is an Armstrong number because it has 3 digits and ${1^3 + 5^3 + 3^3 = 153}$. ### Function Signature ```python def is_armstrong(n: int) -> bool: ``` ### Rules - The exponent is the number of decimal digits of `n` written without leading zeros, so a 3-digit number uses cubes and a 4-digit number uses fourth powers. ### Constraints - `1 <= n <= 10^8` ### Examples ```text Input: n = 153 Output: True ``` 153 has 3 digits, and 1 + 125 + 27 = 153, so it is an Armstrong number. ```text Input: n = 123 Output: False ``` 123 has 3 digits, and 1 + 8 + 27 = 36, which is not 123. ```text Input: n = 7 Output: True ``` 7 has one digit, so the sum is ${7^1 = 7}$. Every single-digit number is an Armstrong number.

Overview: Decide whether a positive integer is an Armstrong (narcissistic) number, meaning it equals the sum of its digits each raised to the power of the digit count. It tests digit extraction with division and modulo, counting digits, and comparing against the untouched original value.

Tech Mahindra logo
Tech Mahindra
Nov 24, 2026
easySoftware EngineerOtherCoding & Algorithms
0
0

Given a positive integer n, return True if n is an Armstrong number (also called a narcissistic number), and False otherwise.

An Armstrong number equals the sum of its digits, each raised to the power of the number of digits it has. For example, 153 is an Armstrong number because it has 3 digits and 13+53+33=153{1^3 + 5^3 + 3^3 = 153}.

Function Signature

def is_armstrong(n: int) -> bool:

Rules

  • The exponent is the number of decimal digits of n written without leading zeros, so a 3-digit number uses cubes and a 4-digit number uses fourth powers.

Constraints

  • 1 <= n <= 10^8

Examples

Input: n = 153
Output: True

153 has 3 digits, and 1 + 125 + 27 = 153, so it is an Armstrong number.

Input: n = 123
Output: False

123 has 3 digits, and 1 + 8 + 27 = 36, which is not 123.

Input: n = 7
Output: True

7 has one digit, so the sum is 71=7{7^1 = 7}. Every single-digit number is an Armstrong number.