DEV Community

loading...

Discussion on: Narcissistic number

Collapse
kspeakman profile image
Kasey Speakman

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