2
\$\begingroup\$

This question already has an answer here:

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

\$\endgroup\$

marked as duplicate by Peter Taylor code-golf Sep 14 '17 at 10:27

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

  • 3
    \$\begingroup\$ What if there is no such numbers, like for n = 2 \$\endgroup\$ – Grzegorz Puławski Sep 14 '17 at 9:49
  • 1
    \$\begingroup\$ 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). \$\endgroup\$ – Leaky Nun Sep 14 '17 at 9:53
  • \$\begingroup\$ @GrzegorzPuławski question updated \$\endgroup\$ – mdahmoune Sep 14 '17 at 9:57
  • \$\begingroup\$ @mdahmoune: Would looping indefinitely be acceptable when there is no number? \$\endgroup\$ – Emigna Sep 14 '17 at 9:59
  • 2
    \$\begingroup\$ This is just a loop round an earlier question, and as such qualifies as a duplicate. \$\endgroup\$ – Peter Taylor Sep 14 '17 at 10:27
2
\$\begingroup\$

05AB1E, 16 15 bytes

Very inefficient.
Empty output if there is no solution.

°LʒDSImOQ}ʒgQ}н

Try it online!

\$\endgroup\$
1
\$\begingroup\$

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.

\$\endgroup\$
  • \$\begingroup\$ 16 bytes: f&qQl`Tqsm^dQjT; \$\endgroup\$ – Mr. Xcoder Sep 14 '17 at 14:19
  • \$\begingroup\$ Abusing String-ification: f&qQl`Tqsm^sdQ` (15 bytes) \$\endgroup\$ – Mr. Xcoder Sep 14 '17 at 14:20
1
\$\begingroup\$

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

\$\endgroup\$

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