Narcissistic Number Checker - Problem

A narcissistic number (also known as an Armstrong number) is an N-digit number that equals the sum of its digits each raised to the power of N.

For example:

  • 153 is a 3-digit narcissistic number: 1³ + 5³ + 3³ = 1 + 125 + 27 = 153
  • 9474 is a 4-digit narcissistic number: 9⁴ + 4⁴ + 7⁴ + 4⁴ = 6561 + 256 + 2401 + 256 = 9474

Write a function to check if a given number is narcissistic. Additionally, find all 3-digit and 4-digit narcissistic numbers.

Input & Output

Example 1 — 3-digit narcissistic number
$ Input: num = 153
Output: true
💡 Note: 153 has 3 digits: 1³ + 5³ + 3³ = 1 + 125 + 27 = 153, which equals the original number
Example 2 — Non-narcissistic number
$ Input: num = 123
Output: false
💡 Note: 123 has 3 digits: 1³ + 2³ + 3³ = 1 + 8 + 27 = 36, which does not equal 123
Example 3 — Single digit (always narcissistic)
$ Input: num = 5
Output: true
💡 Note: 5 has 1 digit: 5¹ = 5, which equals the original number. All single digits are narcissistic.

Constraints

  • 0 ≤ num ≤ 106
  • Input will be a non-negative integer

Visualization

Tap to expand
INPUTALGORITHMRESULT1533-digit number1Count digits: 32Extract: 1, 5, 33Calculate: 1³ + 5³ + 3³4Sum: 1 + 125 + 27 = 153Power calculation:1³=1, 5³=125, 3³=27true153 = 153153 is narcissisticSum equals originalKey Insight:Extract digits mathematically using modulo operations - faster than string conversionTutorialsPoint - Narcissistic Number Checker | Mathematical Method
Asked in
Microsoft 15 Goldman Sachs 12 Adobe 8
12.0K Views
Medium Frequency
~15 min Avg. Time
450 Likes
Ln 1, Col 1
Smart Actions
💡 Explanation
AI Ready
💡 Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen