Test a number for narcissism

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, take 153 (3 digits):

13 + 53 + 33 = 1 + 125 + 27 = 153

1634:

14 + 64 + 34 + 44 = 1 + 1296 + 81 + 256 = 1634

The Challenge:

Your code must take input from the user and output True or False depending upon whether the given number is a Narcissistic Number.

Error checking for text strings or other invalid inputs is not required. 1 or 0 for the output is acceptable. Code that simply generates a list of Narcissistic Numbers, or checks the user input against a list, does not qualify.

OEIS A005188

• Is it ok if I output True if it's such a number, but anything else (in this case the number itself) if not? – devRicher Jan 6 '17 at 21:51

Pyth (non-competing), 10 bytes

qsm^sdlQ

Verify the test cases.

Perl 5, 24 bytes

21 bytes of code + 3 for -pF flags

JavaScript 46 bytes

F=n=>![...n].reduce((x,c)=>x-(+c)**n.length,n)

console.log(F("153") == true)
console.log(F("370") == true)
console.log(F("371") == true)
console.log(F("152") == false)
console.log(F("150") == false)

• Is it necessary to convert c into number? – l4m2 Jan 5 '18 at 4:14

JavaScript 7, 41 Bytes

s=>[...s].map(x=>N+=x**s.length,N=0)|N==s

Pyt, 11 bytes

ĐĐḶ⌊⁺⇹ą⇹^Ʃ=

Explanation:

Implicit input
ĐĐ                    Duplicate the input twice
Ḷ⌊⁺                Get number of digits in the input
⇹ą              Convert input to array of digits
⇹^Ʃ           sum the digits raised to the power of the number of digits
=          equals input?

Try it online!

CJam, 12 bytes

{_Ab_,f#:+=}

Try it online!

Explanation:

{          }   anonymous block, input: 1634
b           get digits in base
A              10: [1 6 3 4]
,         take the length: 4
#       and raise
f          each element
_              of the list of digits
to that power: [1 1296 81 256]
:      fold over
=    and compare
_               to the original: 1 (true)

JavaScript (ES7), 43 38 bytes

Takes input as a string.

n=>n==eval([...n,0].join**n.length+)

Try It

f=
n=>n==eval([...n,0].join**n.length+)
o.innerText=f(i.value="153")
oninput=_=>o.innerText=f(i.value)
<input id=i type=number><pre id=o>

Kotlin, 60 bytes

map{Math.pow(it-'0'+0.0,length+0.0)}.sum()==this.toInt()+0.0

Beautified

map {
Math.pow(it - '0' + 0.0, length + 0.0)
}.sum() == this.toInt() + 0.0

Test

fun String.f() =
map{Math.pow(it-'0'+0.0,length+0.0)}.sum()==this.toInt()+0.0

fun main(args: Array<String>) {
(0..1000).filter { it.toString().f() }.forEach { println(it) }
}

TryItOnline

Javascript (ES6) 46 bytes - Not competing

n=>(s=[...''+n]).forEach(v=>n-=v**s.length)|!n

Explanation:

n=>
(s=[...''+n])        // Convert input to array of chars and assign to s
.forEach(v=>       // Loop over s (returns undefined)
n-=v**s.length)  // reduce input by char^length
|                  // undefined is falsy, so we OR
!n                 // OR with negated input
// returns 1 if 0, 0 otherwise

Pari/GP, 35 bytes

n->n==vecsum([d^#s|d<-s=digits(n)])

Try it online!

Ly, 51 bytes

ns>lS0sp11[ppl:Isp>l<ysp>l<sp>,^<l`sy,=!]>&+s<<l=fp

Try it online!

Tcl, 77 bytes

proc N n {expr [join [lmap d [split $n ""] {expr$d**[string le $n]}] +]==$n}

Try it online!