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


*        Harshad or Niven series - 01/05/2019
NIVEN    CSECT
         USING  NIVEN,R13          base register
         B      72(R15)            skip savearea
         DC     17F'0'             savearea
         SAVE   (14,12)            save previous context
         ST     R13,4(R15)         link backward
         ST     R15,8(R13)         link forward
         LR     R13,R15            set addressability
         LA     R7,2               j=2 
LOOP     MVC    PG,=CL80' '        clear buffer
         LA     R10,PG             @pg
         LA     R8,0               n=0 
       IF C,R7,EQ,=A(2) THEN       if j=2
         LA     R9,20                nn=20
         LA     R6,1                 i=1 
       ELSE     ,                  else         
         LA     R9,1                 nn=1
         LA     R6,1001              i=1001 
       ENDIF    ,                  end if
       DO WHILE=(CR,R8,LT,R9)      do i=1 by 1 while(n<nn)
         BAL    R14,HARSHAD          call harshad(i)
       IF   LTR,R1,Z,R1 THEN         if rc=0 then
         LA     R8,1(R8)               n++
         XDECO  R6,XDEC                edit i
         MVC    0(4,R10),XDEC+8        output i
         LA     R10,4(R10)             @pg+=4
       ENDIF    ,                    end if
         LA     R6,1(R6)             i++ 
       ENDDO    ,                  enddo i
         XPRNT  PG,L'PG            print buffer
         BCT    R7,LOOP            j=j-1; loop if j<>0
         L      R13,4(0,R13)       restore previous savearea pointer
         RETURN (14,12),RC=0       restore registers from calling sav
HARSHAD  EQU    *                  harshad(i)
         CVD    R6,PACKED          convert to packed   PL8
         UNPK   ZONED,PACKED       packed PL8 to zoned ZL16
         LA     R1,ZONED           @c
         XR     R4,R4              sum=0; m=1
       DO WHILE=(C,R1,LE,=A(ZONED+15)) do m=1 to 16
         NI     0(R1),X'0F'          c(m) : character to integer
         XR     R2,R2                ~
         IC     R2,0(R1)             c(m)
         AR     R4,R2                sum=sum+c(m)
         LA     R1,1(R1)             @c++
       ENDDO    ,                  enddo m
         XR     R2,R2              ~
         LR     R3,R6              i
         DR     R2,R4              i/sum     
         LR     R1,R2              rc=mod(i,sum)
         BR     R14                return to caller
PACKED   DS     PL8                packed decimal (15num)
ZONED    DS     ZL16               zoned  decimal (16num)
PG       DS     CL80               buffer
XDEC     DS     CL12               temp xdeco
         REGEQU                    symbolic registers
         END    NIVEN