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


 
for i = 1 to 5000
    if Munch(i) Then
        Wscript.Echo i, "is a Munchausen number"
    end if
next
 
'Returns True if num is a Munchausen number. This is true if the sum of
'each digit raised to that digit's power is equal to the given number.
'Example: 3435 = 3^3 + 4^4 + 3^3 + 5^5
 
Function Munch (num)
 
    dim str: str = Cstr(num)    'input num as a string
    dim sum: sum = 0            'running sum of n^n
    dim i                       'loop index
    dim n                       'extracted digit
 
    for i = 1 to len(str)
        n = CInt(Mid(str,i,1))
        sum = sum + n^n
    next
 
    Munch = (sum = num)
 
End Function
 