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


;there are considered digits 0-5 because 6^6>5000
DEFINE MAXDIGIT="5"
INT ARRAY powers(MAXDIGIT+1)
 
INT FUNC Power(BYTE x)
  INT res
  BYTE i
 
  IF x=0 THEN RETURN (0) FI
  res=1
  FOR i=0 TO x-1
  DO
    res==*x
  OD
RETURN (res)
 
BYTE FUNC IsMunchausen(INT x)
  INT sum,tmp
  BYTE d
 
  tmp=x sum=0
  WHILE tmp#0
  DO
    d=tmp MOD 10
    IF d>MAXDIGIT THEN
      RETURN (0)
    FI
    sum==+powers(d)
    tmp==/10
  OD
  IF sum=x THEN
    RETURN (1)
  FI
RETURN (0)
 
PROC Main()
  INT i
 
  FOR i=0 TO MAXDIGIT
  DO
    powers(i)=Power(i)
  OD
  FOR i=1 TO 5000
  DO
    IF IsMunchausen(i) THEN
      PrintIE(i)
    FI
  OD  
RETURN