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


' FB 1.05.0 Win64
 
Function sumDigits(n As Integer) As Integer
  If n < 0 Then Return 0
  Dim sum As Integer
  While n > 0
    sum += n Mod 10
    n \= 10
  Wend
  Return sum
End Function
 
Function isHarshad(n As Integer) As Boolean
  Return n Mod sumDigits(n) = 0
End Function
 
Print "The first 20 Harshad or Niven numbers are :"
Dim count As Integer = 0
Dim i As Integer = 1
 
Do
  If isHarshad(i) Then
    Print i; " ";
    Count += 1
    If count = 20 Then Exit Do
  End If
  i += 1
Loop
 
Print : Print
Print "The first such number above 1000 is :"
i = 1001
 
Do  
  If isHarshad(i) Then 
    Print i; " "
    Exit Do
  End If
  i += 1
Loop
 
Print
Print "Press any key to quit"
Sleep