A Narcissistic Number is a number that is the sum of its own digits, each one raised to the power of the number of digits.
Example: 1^3 + 5^3 + 3^3 = 1 + 125 + 27 = 153
To practice! :D
A Narcissistic Number is a number that is the sum of its own digits, each one raised to the power of the number of digits.
Example: 1^3 + 5^3 + 3^3 = 1 + 125 + 27 = 153
To practice! :D
For further actions, you may consider blocking this person and/or reporting abuse
Ogasawara Kakeru -
Sam -
Kfir -
saifalikhan9 -
Top comments (11)
Here is a little implementation in Python3.6:
Display:
Awesome answer, Pierre!
I have one doubt though. Wouldn't it be better to first convert
nb
into a string, and then just pass the string to the list comprehension, instead of doingstr(nb)
for every iteration? That is,This might be a little more efficient. Also, we can remove the list comprehension inside and replace it with a generator.
This is fun! Here is a Python 3.5 one-liner with only one calculation of the length of nb per nb :
print([nb for nb in range(10000) if nb == sum(int(c)**l for l in [len(str(nb))] for c in str(nb))])
And now with also one conversion of str(nb) per nb:
print([nb for nb in range(10000) if nb == sum(int(c)**l for str_nb in [str(nb)] for l in [len(str_nb)] for c in str_nb)])
Yeah you're right! Thank you, that's clever!
I'm learning Rust and this challenge looks good to practice, this is my solution.
Here's my little code golf in javascript.
My Rust solution.
Good solution!!, but if you test the code here doesnt run, only works in a more updated version no ? What version do you use ?
The inclusive range (..=) that I've used is available from Rust 1.24. It will work with previous versions. But you can change it from
(0..=1_000_000)
to(0..1_000_001)
and it should work in any previous version.repl.it uses Rust 1.9, the
for_each
method for iterator is avalable from 1.21 onwards. You can save the filtered list and callfor
loop on elements.Here's the repl.it link with formatted code, that will work over there. link
If you want to run the newer version, here's a link to rust playground with the code.
Have fun 😃
An Elm Solution
Algorithm
Usage
Results
Here are the narcissists up to 10 billion.
0 1 2 3 4 5 6 7 8 9 153 370 371 407 1634 8208 9474 54748 92727 93084 548834 1741725 4210818 9800817 9926315 24678050 24678051 88593477 146511208 472335975 534494836 912985153 4679307774
Dont know if this is still up for discussion but here is my code snippet in JS hope it helps...