The Harshad or Niven numbers are positive integers ≥ 1 that are divisible by the sum of their digits.

For example,   42   is a Harshad number as   42   is divisible by   (4 + 2)   without remainder.

Assume that the series is defined as the numbers in increasing order.



Task

The task is to create a function/method/procedure to generate successive members of the Harshad sequence.

Use it to:

   list the first 20 members of the sequence,   and
   list the first Harshad number greater than 1000.


Show your output here.



Related task
   Increasing gaps between consecutive Niven numbers


See also
    OEIS: A005349


*process source or(!) xref attributes;
 niven: Proc Options(main);
 /*********************************************************************
 * 08-06.2013 Walter Pachl translated from Rexx
 *                         with a slight improvement:  Do j=y+1 By 1;
 *********************************************************************/
 Dcl (ADDR,HBOUND,MOD,SUBSTR,VERIFY) Builtin;
 Dcl SYSPRINT Print;
 
 Dcl (x,y) dec fixed(8);
 x=20;
 y=1000;
 Begin;
   Dcl (n(x),j) Dec Fixed(8);
   Dcl ni Bin Fixed(31) Init(0);
   Dcl result Char(100) Var Init('');
 loop:
   Do j=1 By 1;
     If mod(j,sumdigs(j))=0 Then Do;
       ni+=1;
       n(ni)=j;
       result=result!!' '!!d2c(j);
       If ni=x Then Leave loop;
       End;
     End;
   Put Edit('first 20 Niven numbers: ',result)(Skip,a,a);
   Do j=y+1 By 1;
     If mod(j,sumdigs(j))=0 Then
       Leave;
     End;
   Put Edit('first Niven number > ',d2c(y),' is: ',d2c(j))(Skip,4(a));
   End;
 
 sumDigs: proc(z) Returns(Dec Fixed(3));
 Dcl z Pic'(8)9';
 Dcl d(8) Pic'9' Based(addr(z));
 Dcl i Bin Fixed(31);
 Dcl sd Dec Fixed(3) Init(0);
 Do i=1 To hbound(d);
   sd+=d(i);
   End;
 Return(sd);
 End;
 
 d2c: Proc(z) Returns(char(8) Var);
 Dcl z Pic'(8)z';
 Dcl p Bin Fixed(31);
 p=verify(z,' ');
 Return(substr(z,p));
 End;
 
 End;