A   Narcissistic decimal number   is a non-negative integer,   



n


{\displaystyle n}

,   that is equal to the sum of the   



m


{\displaystyle m}

-th   powers of each of the digits in the decimal representation of   



n


{\displaystyle n}

,   where   



m


{\displaystyle m}

   is the number of digits in the decimal representation of   



n


{\displaystyle n}

.



Narcissistic (decimal) numbers are sometimes called   Armstrong   numbers, named after Michael F. Armstrong.

They are also known as   Plus Perfect   numbers.



An example

   if   



n


{\displaystyle n}

   is   153 
   then   



m


{\displaystyle m}

,   (the number of decimal digits)   is   3 
   we have    13 + 53 + 33   =   1 + 125 + 27   =   153  
   and so   153   is a narcissistic decimal number


Task

Generate and show here the first   25   narcissistic decimal numbers.



Note:   




0

1


=
0


{\displaystyle 0^{1}=0}

,   the first in the series.



See also

   the  OEIS entry:     Armstrong (or Plus Perfect, or narcissistic) numbers.
   MathWorld entry:   Narcissistic Number.
   Wikipedia entry:     Narcissistic number.


defmodule RC do
  def narcissistic(m) do
    Enum.reduce(1..10, [0], fn digits,acc ->
      digitPowers = List.to_tuple(for i <- 0..9, do: power(i, digits))
      Enum.reduce(power(10, digits-1) .. power(10, digits)-1, acc, fn n,result ->
        sum = divsum(n, digitPowers, 0)
        if n == sum do
          if length(result) == m-1, do: throw Enum.reverse(result, [n])
          [n | result]
        else
          result
        end
      end)
    end)
  end
 
  defp divsum(0, _, sum), do: sum
  defp divsum(n, digitPowers, sum) do
    divsum(div(n,10), digitPowers, sum+elem(digitPowers,rem(n,10)))
  end
 
  defp power(n, m), do: power(n, m, 1)
 
  defp power(_, 0, pow), do: pow
  defp power(n, m, pow), do: power(n, m-1, pow*n)
end
 
try do
  RC.narcissistic(25)
catch
  x -> IO.inspect x
end