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


digits = iter (n: int) yields (int)
    while n>0 do
        yield(n//10)
        n := n/10
    end
end digits
 
munchausen = proc (n: int) returns (bool)
    k: int := 0
    for d: int in digits(n) do
        % Note: 0^0 is to be regarded as 0
        if d~=0 then k := k + d ** d end 
    end
    return(n = k)
end munchausen
 
start_up = proc ()
    po: stream := stream$primary_output()
    for i: int in int$from_to(1,5000) do
        if munchausen(i) then stream$putl(po, int$unparse(i)) end
    end
end start_up