53
\$\begingroup\$

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

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

73 Answers 73

0
\$\begingroup\$

Pyth (non-competing), 10 bytes

qsm^sdl`Q`

Verify the test cases.

\$\endgroup\$
0
\$\begingroup\$

Perl 5, 24 bytes

21 bytes of code + 3 for -pF flags

for$i(@F){$_-=$i**@F}

Try it online!

\$\endgroup\$
0
\$\begingroup\$

Swift 3.2: 107 characters

Of course Swift is absolutely not a quick language but I thought I would try:

func n(s:String){let c=s.utf8.map{Double($0)-48};print(c.reduce(0){$0+pow($1,Double(c.count))}==Double(s))}
\$\endgroup\$
0
\$\begingroup\$

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)

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

JavaScript 7, 41 Bytes

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

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!

\$\endgroup\$
0
\$\begingroup\$

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
         +       addition: 1634
          =    and compare
 _               to the original: 1 (true)
\$\endgroup\$
0
\$\begingroup\$

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>

\$\endgroup\$
0
\$\begingroup\$

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) }
}

TIO

TryItOnline

\$\endgroup\$
0
\$\begingroup\$

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
\$\endgroup\$
0
\$\begingroup\$

Pari/GP, 35 bytes

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

Try it online!

\$\endgroup\$
0
\$\begingroup\$

Ly, 51 bytes

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

Try it online!

\$\endgroup\$
0
\$\begingroup\$

Tcl, 77 bytes

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

Try it online!

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.