A Disarium number is an integer where the sum of each digit raised to the power of its position in the number, is equal to the number.



E.G.
135 is a Disarium number:

 11 + 32 + 53 == 1 + 9 + 125 == 135

There are a finite number of Disarium numbers.



Task
 Find and display the first 18 Disarium numbers.


Stretch
 Find and display all 20 Disarium numbers.


See also
 Geeks for Geeks - Disarium numbers
 OEIS:A032799 - Numbers n such that n equals the sum of its digits raised to the consecutive powers (1,2,3,...)
 Related task: Narcissistic decimal number
 Related task: Own digits power sum Which seems to be the same task as Narcissistic decimal number...




100H: /* FIND SOME DISARIUM NUMBERS - NUMBERS WHOSE DIGIT POSITION-POWER     */
      /* SUMS ARE EQUAL TO THE NUMBER, E.G. 135 = 1^1 + 3^2 + 5^3            */
 
   /* CP/M BDOS SYSTEM CALL, IGNORE THE RETURN VALUE                         */
   BDOS: PROCEDURE( FN, ARG ); DECLARE FN BYTE, ARG ADDRESS; GOTO 5; END;
   PR$CHAR:   PROCEDURE( C ); DECLARE C BYTE;    CALL BDOS( 2, C );  END;
   PR$STRING: PROCEDURE( S ); DECLARE S ADDRESS; CALL BDOS( 9, S );  END;
   PR$NUMBER: PROCEDURE( N ); /* PRINTS A NUMBER IN THE MINIMUN FIELD WIDTH  */
      DECLARE N ADDRESS;
      DECLARE V ADDRESS, N$STR ( 6 )BYTE, W BYTE;
      V = N;
      W = LAST( N$STR );
      N$STR( W ) = '$';
      N$STR( W := W - 1 ) = '0' + ( V MOD 10 );
      DO WHILE( ( V := V / 10 ) > 0 );
         N$STR( W := W - 1 ) = '0' + ( V MOD 10 );
      END;
      CALL PR$STRING( .N$STR( W ) );
   END PR$NUMBER;
 
   /* TABLE OF POWERS UP TO THE FOURTH POWER - AS WE ARE ONLY FINDING THE   */
   /* DISARIUM NUMBERS UP TO 9999                                           */
   DECLARE POWER( 40 /* ( 1 : 4, 0 : 9 ) */ ) ADDRESS;
   DECLARE MAX$DISARIUM LITERALLY '9999';
 
   DECLARE ( N, D, POWER$OF$TEN, COUNT, LENGTH, V, P, DPS, NSUB, NPREV )
                                              ADDRESS;
 
   /* COMPUTE THE NTH POWERS OF 0-9                                         */
   DO D = 0 TO 9; POWER( D ) = D; END;
   NSUB  = 10;
   NPREV =  0;
   DO N = 2 TO 4;
      POWER( NSUB ) = 0;
      DO D = 1 TO 9;
         POWER( NSUB + D ) = POWER( NPREV + D ) * D;
      END;
      NPREV = NSUB;
      NSUB  = NSUB + 10;
   END;
 
   /* PRINT THE DISARIUM NUMBERS UPTO 9999 OR THE 18TH, WHICHEVER IS SOONER */
   POWER$OF$TEN = 10;
   LENGTH       =  1;
   COUNT, N     =  0;
   DO WHILE( N < MAX$DISARIUM AND COUNT < 18 );
      IF N = POWER$OF$TEN THEN DO;
         /* THE NUMBER OF DIGITS JUST INCREASED                             */
         POWER$OF$TEN = POWER$OF$TEN * 10;
         LENGTH       = LENGTH       +  1;
      END;
      /* FORM THE DIGIT POWER SUM                                           */
      V   = N;
      P   = LENGTH * 10;
      DPS = 0;
      DO D = 1 TO LENGTH;
         P = P - 10;
         DPS = DPS + POWER( P + ( V MOD 10 ) );
         V = V / 10;
      END;
      IF DPS = N THEN DO;
         /* N IS DISARIUM                                                   */
         COUNT = COUNT + 1;
         CALL PR$CHAR( ' ' );
         CALL PR$NUMBER( N );
      END;
      N = N + 1;
   END;
 
EOF