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.


       program nivengaps      
       implicit none
       integer*8 prev /1/, gap /0/, sum /0/
       integer*8 nividx /0/, niven /1/
       integer gapidx /1/
 
       character*13 idxfmt
       character*14 nivfmt
       write (*,*) 'Gap no  Gap   Niven index    Niven number '
       write (*,*) '------  ---  -------------  --------------'
 
 10    call divsum(niven, sum)
       if (mod(niven, sum) .EQ. 0) then
          if (niven .GT. prev + gap) then
             gap = niven - prev
             call fmtint(nividx,13,idxfmt)
             call fmtint(prev,14,nivfmt)
             write (*,20) gapidx,gap,idxfmt,nivfmt
             gapidx = gapidx + 1
          end if
          prev = niven
          nividx = nividx + 1
       end if          
       niven = niven + 1
       if (gapidx .LE. 32) go to 10
 
       stop
 20    format (i7,'  ',i3,'  ',a13,'  ',a14)       
       end program
 
C      Sum of divisors of NN, given the sum of divisors of NN-1       
       subroutine divsum(nn,sum)
          implicit none
          integer*8 n,nn,sum
          n = nn          
          sum = sum + 1
 30       if (n.GT.0 .AND. mod(n,10).EQ.0) then
             sum = sum - 9
             n = n / 10
             go to 30
          end if
       end subroutine  
 
       integer*8 function mod(a,b)
          implicit none
          integer*8 a,b
          mod = a - a/b * b
       end function
 
C      Format a positive integer with ',' as the thousands separator.
       subroutine fmtint(num, len, str)
          implicit none 
          integer*8 n, num
          integer pos, len, th
          character(*) str
          n=num
          pos=len
          th=2
 40       if (pos.GT.0) then
             if (n.EQ.0) then
                str(pos:pos) = ' '
             else
                str(pos:pos) = achar(mod(n,10) + iachar('0'))
                if (th.EQ.0 .AND. n.GE.10 .AND. pos.GT.1) then
                    th = 2
                    pos = pos-1
                    str(pos:pos) = ','
                else
                    th = th-1
                end if
            end if
            pos = pos - 1
            n = n/10
            go to 40
          end if    
       end subroutine