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: 13 + 53 + 33 = 1 + 125 + 27 = 153

To practice! :D

Did you find this post useful? Show some love!

Here is a little implementation in Python3.6:

def is_narcissistic(nb: int) -> bool:
    return nb == sum([int(c)**len(str(nb)) for c in str(nb)])


if __name__ == '__main__':
    print([nb for nb in range(10000) if is_narcissistic(nb)])

Display:

[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 153, 370, 371, 407, 1634, 8208, 9474]

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 doing str(nb) for every iteration? That is,

str_nb = str(nb)
return nb == sum(int(c)**len(str_nb) for c in str_nb)

This might be a little more efficient. Also, we can remove the list comprehension inside and replace it with a generator.

Yeah you're right! Thank you, that's clever!

I'm learning Rust and this challenge looks good to practice, this is my solution.

fn main() {
  for number in 0..1000 {
    if is_narcissistic(number) {
      println!("{} is narcissistic", number);
    }
  }
}

fn is_narcissistic(number: i32) -> bool {
  digits_pow(number) == number
}

fn digits_pow(mut number: i32) -> i32 {
  let digits= (number as f64).log10()
    .floor() + 1_f64;

  let mut sum = 0;

  while number != 0 {
    sum += ((number % 10)).pow(digits as u32);
    number = ((number / 10) as f64).floor() as i32;
  }

  sum
}
0 is narcissistic
1 is narcissistic
2 is narcissistic
3 is narcissistic
4 is narcissistic
5 is narcissistic
6 is narcissistic
7 is narcissistic
8 is narcissistic
9 is narcissistic
153 is narcissistic
370 is narcissistic
371 is narcissistic
407 is narcissistic

Here's my little code golf in javascript.

const digit_pow = (n, pow = n.toString().length, sum = 0) =>
  n ? digit_pow(Math.floor(n / 10), pow, sum + (n % 10) ** pow) : sum;

for (let i = 0; i < 1e6; i++) {
  if (digit_pow(i) === i) console.log(i);
}
Ben Halpern DEV.TO FOUNDER

Hey there, we see you aren't signed in. (Yes you, the reader. This is a fake comment.)

Please consider creating an account on dev.to. It literally takes a few seconds and we'd appreciate the support so much. ❤️

Plus, no fake comments when you're signed in. 🙃

My Rust solution.

fn is_narc(n: i32) -> bool {
  let digits = n.to_string().len() as u32;
  let mut sum = 0;
  let mut m = n;
  while m != 0 {
    sum += (m % 10).pow(digits);
    m /= 10;
  }
  sum == n
}

// Usage
fn main() {
  (0..=1_000_000)         // upto 1 million
    .filter(|&x| is_narc(x))
    .for_each(|x| println!("{}", x));
}

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 call for 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

narcSum digits sum todo =
  if todo == 0 then
    sum
  else
    narcSum digits ((todo % 10) ^ digits + sum) (todo // 10)

isNarc a =
  a == narcSum (String.length (toString a)) 0 a

Usage

List.range 0 1000000
  |> List.filter isNarc

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

Classic DEV Post from Sep 8 '17

What's your favorite useless repo/package/website/etc?

So I'm trying to remedy my carpal tunnel by considering making the decision to ...

READ POST
Follow @andy to see more of their posts in your feed.
José Oscátegui
I'm a front end developer 😃
More from @josktgui
Check if the chain has the same amount
#challenge #javascript #python #etc
Challenges
#challenge #javascript #python #etc
Trending on dev.to
UWP, Electron, and Desktop development with javascript; some thoughts.
#javascript #electron #uwp
Deploying a React app with React-Router and an Express Backend
#express #react #reactrouter #javascript
Project Euler #3 - Largest Prime Factor
#projecteuler #challenge
Web Maker - From vanilla JS to Preact
#preact #javascript #react #refactor
Context in React
#react #javascript
Online NPM downloads calculator
#javascript #npm
Coding Concepts - Generics
#webdev #typescript #beginners #javascript
Isomorphic ES Modules
#webdev #javascript #isomorphic #esmodules