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


with Ada.Text_IO;
 
procedure Munchausen is
 
   function Is_Munchausen (M : in Natural) return Boolean is
      Table : constant array (Character range '0' .. '9') of Natural :=
        (0**0, 1**1, 2**2, 3**3, 4**4,
         5**5, 6**6, 7**7, 8**8, 9**9);
      Image : constant String := M'Image;
      Sum   : Natural := 0;
   begin
      for I in Image'First + 1 .. Image'Last loop
         Sum := Sum + Table (Image (I));
      end loop;
      return Image = Sum'Image;
   end Is_Munchausen;
 
begin
   for M in 1 .. 5_000 loop
      if Is_Munchausen (M) then
         Ada.Text_IO.Put (M'Image);
      end if;
   end loop;
   Ada.Text_IO.New_Line;
end Munchausen;