A Munchausen number is a natural number n the sum of whose digits (in base 10), each raised to the power of itself, equals n.

(Munchausen is also spelled: Münchhausen.)

For instance:    3435 = 33 + 44 + 33 + 55 



Task
Find all Munchausen numbers between   1   and   5000.



Also see

 The OEIS entry:  A046253
 The Wikipedia entry:  Perfect digit-to-digit invariant, redirected from Munchausen Number


 
Module Munchausen {
      Inventory p=0:=0,1:=1
      for i=2 to 9 {Append p, i:=i**i}
      Munchausen=lambda p (x)-> {
            m=0
            t=x
            do {
                  m+=p(x mod 10)
                  x=x div 10
            } until x=0
            =m=t
      }
      For i=1 to 5000
            If Munchausen(i) then print i,
      Next i
      Print
}
Munchausen
 