Harshad or Niven series
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.
The task is to create a function/method/procedure to generate successive members of the Harshad sequence. Use it to list the first twenty members of the sequence. List the first Harshad number greater than 1000. Show your output here.
Python
<lang python>>>> import itertools >>> def harshad(): n=1 while True: if n % sum(int(ch) for ch in str(n)) == 0: yield n n += 1
>>> list(itertools.islice(harshad(), 0, 20))
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 12, 18, 20, 21, 24, 27, 30, 36, 40, 42]
>>> for n in harshad():
if n > 1000:
print(n)
break
1002
>>> </lang>