# Smallest Narcissistic Number [duplicate]

Given a natural numbers n>1, find the smallest narcissistic number of n digit.

A narcissistic number is a number which is the sum of its own digits, each raised to the power of the number of digits.

For example, for n=3 (3 digits) the out put should be 153:

1^3 + 5^3 + 3^3 = 1 + 125 + 27 = 153

For n=4 (4 digits) the out put should be 1634:

1^4 + 6^4 + 3^4 + 4^4 = 1 + 1296 + 81 + 256 = 1634

For n=5 (5 digits) the out put should be 54748:

5^5 + 4^5 + 7^5 + 4^5 + 8^5 = 54748

If there is no such numbers, like for example n = 2 or n = 22 output any special output (a negative number, an exception, an error, empty,...).

Winning Criteria

This is , so shortest answer in bytes by language wins.

OEIS A005188

• What if there is no such numbers, like for n = 2 – Grzegorz Puławski Sep 14 '17 at 9:49
• We've tested whether a number is narcissistic, and then I've asked you guys to output all narcissistic numbers (because there are only 88 of them). – Leaky Nun Sep 14 '17 at 9:53
• @GrzegorzPuławski question updated – mdahmoune Sep 14 '17 at 9:57
• @mdahmoune: Would looping indefinitely be acceptable when there is no number? – Emigna Sep 14 '17 at 9:59
• This is just a loop round an earlier question, and as such qualifies as a duplicate. – Peter Taylor Sep 14 '17 at 10:27

# 05AB1E, 16 15 bytes

Very inefficient.
Empty output if there is no solution.

°LʒDSImOQ}ʒgQ}н


Try it online!

# Pyth, 20 bytes

hfqTsm^dQjT10r^TtQ^T


Try it online!

How it works...

hfqTsm^dQjT10r^TtQ^T    Implicit: Q=input()

^T    10^Q (final Q is inferred)
^TtQ      10^(Q-1)
r          Range over the above
(generates list of numbers of length Q)
f                      Filter each element in the above (as T) over...
jT10              Get digits in T
m                     Map each digit in the above (as d) over...
^dQ                     d^Q
s                      Sum these
qT                       Is the above equal to the original number?
h                       Take the first element of this filtered list


Throws an error if no solutions exist.

• 16 bytes: f&qQlTqsm^dQjT; – Mr. Xcoder Sep 14 '17 at 14:19
• Abusing String-ification: f&qQlTqsm^sdQ (15 bytes) – Mr. Xcoder Sep 14 '17 at 14:20

# JavaScript (ES7), 82 79 bytes

Saved 3 bytes thanks to @ThePirateBay

Returns undefined when there's no solution. Reasonably fast up to n = 7 and really slow beyond that.

n=>[...Array(10**n).keys()].find(x=>x==eval([...x+='0'].join(**${n}+))&&x[n])  ### Demo let f = n=>[...Array(10**n).keys()].find(x=>x==eval([...x+='0'].join(**${n}+))&&x[n])

for(n = 2; n < 6; n++) {
console.log(n, f(n))
}

# Recursive version, 72 bytes

Returns "0" (as a string) when there's no solution. For n > 4, it would require to either enable Tail-Call-Optimization (not tested) or extend the maximum size of the call stack.

f=(n,x=1)=>eval([...s=x+'0'].join(**${n}+))-x|!s[n]?s[n+1]||f(n,x+1):x  f=(n,x=1)=>eval([...s=x+'0'].join(**${n}+))-x|!s[n]?s[n+1]||f(n,x+1):x

for(n = 2; n < 5; n++) {
console.log(n, f(n))
}`