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


 
Option Explicit
 
Sub Main_Munchausen_numbers()
Dim i&
 
    For i = 1 To 5000
        If IsMunchausen(i) Then Debug.Print i & " is a munchausen number."
    Next i
End Sub
 
Function IsMunchausen(Number As Long) As Boolean
Dim Digits, i As Byte, Tot As Long
 
    Digits = Split(StrConv(Number, vbUnicode), Chr(0))
    For i = 0 To UBound(Digits) - 1
        Tot = (Digits(i) ^ Digits(i)) + Tot
    Next i
    IsMunchausen = (Tot = Number)
End Function
 