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


       IDENTIFICATION DIVISION.
       PROGRAM-ID.  MUNCHAUSEN.
 
       DATA DIVISION.
       WORKING-STORAGE SECTION.
       01 VARIABLES.
          03 CANDIDATE   PIC 9(4).
          03 DIGITS      PIC 9 OCCURS 4 TIMES, REDEFINES CANDIDATE.
          03 DIGIT       PIC 9.
          03 POWER-SUM   PIC 9(5).
 
       01 OUTPUT-LINE.
          03 OUT-NUM     PIC ZZZ9.
 
       PROCEDURE DIVISION.
       BEGIN.
           PERFORM MUNCHAUSEN-TEST VARYING CANDIDATE FROM 1 BY 1
               UNTIL CANDIDATE IS GREATER THAN 6000.
           STOP RUN.
 
       MUNCHAUSEN-TEST.
           MOVE ZERO TO POWER-SUM.
           MOVE 1 TO DIGIT.
           INSPECT CANDIDATE TALLYING DIGIT FOR LEADING '0'.
           PERFORM ADD-DIGIT-POWER VARYING DIGIT FROM DIGIT BY 1
               UNTIL DIGIT IS GREATER THAN 4.
           IF POWER-SUM IS EQUAL TO CANDIDATE,
               MOVE CANDIDATE TO OUT-NUM,
               DISPLAY OUTPUT-LINE.
 
       ADD-DIGIT-POWER.
           COMPUTE POWER-SUM = 
                     POWER-SUM + DIGITS(DIGIT) ** DIGITS(DIGIT)