Note:   Niven   numbers are also called   Harshad   numbers.

   They are also called    multidigital   numbers.

Niven numbers are positive integers which are evenly divisible by the sum of its
digits   (expressed in base ten).

Evenly divisible   means   divisible with no remainder.



Task

   find the gap (difference) of a Niven number from the previous Niven number
   if the gap is   larger   than the (highest) previous gap,   then:
   show the index (occurrence) of the gap     (the 1st gap is   1)
   show the index of the Niven number that starts the gap     (1st Niven number is   1,   33rd Niven number is   100)
   show the Niven number that starts the gap
   show all numbers with comma separators where appropriate   (optional)
   I.E.:   the gap size of   60   starts at the   33,494th   Niven number which is Niven number   297,864
   show all increasing gaps up to the   ten millionth   (10,000,000th)   Niven number
   (optional)   show all gaps up to whatever limit is feasible/practical/realistic/reasonable/sensible/viable on your computer
   show all output here, on this page


Related task

   Harshad or Niven series.


Also see

   Journal of Integer Sequences, Vol. 6 (2004), Article 03.2.5, Large and Small Gaps Between Consecutive Niven Numbers.
   (PDF) version of the (above) article by Doyon.


 
function digit_sum(n, sum)
	# devuelve la suma de los dígitos de n dada la suma de los dígitos de n - 1
	sum ++
	while (n > 0 and n mod 10 = 0)
		sum -= 9
		n = int(n / 10)
	end while
	return sum
end function
 
function divisible(n, d)
	if ((d mod 1) = 0) and ((n mod 1) = 1) then
		return 0
	end if
	return (n mod d = 0)
end function
 
global sum
sum = 0
gap = 0 : niven = 0 : niven_index = 0
gap_index = 0
previous  = 1
print "Gap index    Gap      Niven index     Niven number"
print "---------    ---      -----------     ------------"
 
for niven = 1 to 1000000
	sum = digit_sum(niven, sum)
	if divisible(niven, sum) then
		if (niven > previous + gap) then
			gap_index += 1
			gap = niven - previous
			print gap_index, gap, niven_index, previous
		end if
		previous = niven
		niven_index ++
	end if
next niven
end
 