Test a number for narcissism

APL (15)

∆≡⍕+/(⍎¨∆)*⍴∆←⍞

Outputs 1 if true and 0 if false.

Explanation:

  • ∆←⍞: read a line (as characters), store in
  • (⍎¨∆)*⍴∆: evaluate each character in and raise it to the power ⍴∆
  • ∆≡⍕+/: see if the input equals the string representation of the sum of these

GolfScript, 16 characters

~.`:s{48-s,?-}/!

Input must be given on STDIN, output is 0 or 1 indicating non-narcissistic / narcissistic number.

Explanation of the code:

~              # Evaluate the input to get a number
.              # Accumulator (initially the number itself)
`:s            # Convert number to string and assign to variable s
{              # Loop over characters of the string
  48-          # Reduce character value by 48
  s,           # Push length of input number
  ?            # Power
  -            # Subtract result from accumulator
}/
!              # Not! (i.e. iff accumulator was zero it was a narcissistic number)

Mathematica, 43 chars

Tr[#^Length@#&@IntegerDigits@#]==#&@Input[]