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


munchausen: procedure options(main);
    /* precalculate powers */
    declare (pows(0:5), i) fixed;
    pows(0) = 0; /* 0^0=0 for Munchausen numbers */
    do i=1 to 5; pows(i) = i**i; end;
 
    declare (d1, d2, d3, d4, num, dpow) fixed;
    do d1=0 to 5;
        do d2=0 to 5;
            do d3=0 to 5;
                do d4=1 to 5;
                    num = d1*1000 + d2*100 + d3*10 + d4;
                    dpow = pows(d1) + pows(d2) + pows(d3) + pows(d4);
                    if num=dpow then put skip list(num);
                end;
            end;
        end;
    end;
end munchausen;