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...




begin % find some Disarium numbers - numbers whose digit position-power sums  %
      % are equal to the number, e.g. 135 = 1^1 + 3^2 + 5^3                   %
    integer array power ( 1 :: 9, 0 :: 9 );
    integer MAX_DISARIUM;
    integer count, powerOfTen, length, n;
    % compute the nth powers of 0-9                                           %
    for d := 0 until 9 do power( 1, d ) := d;
    for n := 2 until 9 do begin
        power( n, 0 ) := 0;
        for d := 1 until 9 do power( n, d ) := power( n - 1, d ) * d
    end for_n;
    % print the first few Disarium numbers                                    %
    MAX_DISARIUM := 19;
    count        :=  0;
    powerOfTen   := 10;
    length       :=  1;
    n            :=  0;
    while count < MAX_DISARIUM do begin
        integer v, dps;
        if n = powerOfTen then begin
            % the number of digits just increased                             %
            powerOfTen := powerOfTen * 10;
            length     := length     +  1
        end if_m_eq_powerOfTen ;
        % form the digit power sum                                            %
        v   := n;
        dps := 0;
        for p := length step -1 until 1 do begin
            dps := dps + power( p, v rem 10 );
            v   := v div 10;
        end FOR_P;
        if dps = n then begin
            % n is Disarium                                                   %
            count := count + 1;
            writeon( i_w := 1, s_w := 0, " ", n )
        end if_dps_eq_n ;
        n := n + 1
    end
end.