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


include "cowgol.coh";
 
sub digitPowerSum(n: uint16): (sum: uint32) is
    var powers: uint32[10] := 
        {1, 1, 4, 27, 256, 3125, 46656, 823543, 16777216, 387420489};
 
    sum := 0;
    loop
        sum := sum + powers[(n % 10) as uint8];
        n := n / 10;
        if n == 0 then break; end if;
    end loop;
end sub;
 
var n: uint16 := 1;
while n < 5000 loop
    if n as uint32 == digitPowerSum(n) then
        print_i16(n);
        print_nl();
    end if;
    n := n + 1;
end loop;