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


' FB 1.05.0 Win64
' Cache n ^ n for the digits 1 to 9
' Note than 0 ^ 0 specially treated as 0 (not 1) for this purpose
Dim Shared powers(1 To 9) As UInteger
For i As UInteger = 1 To 9 
  Dim power As UInteger = i
  For j As UInteger = 2 To i
     power *= i
  Next j
  powers(i) = power
Next i
 
Function isMunchausen(n As UInteger) As Boolean
  Dim p As UInteger = n
  Dim As UInteger digit, sum
  While p > 0
    digit = p Mod 10
    If digit > 0 Then sum += powers(digit)
    p \= 10
  Wend
  Return n = sum
End Function  
 
Print "The Munchausen numbers between 0 and 500000000 are : "
For i As UInteger = 0 To 500000000
  If isMunchausen(i) Then Print i
Next
 
Print
Print "Press any key to quit"
 
Sleep