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.


include "cowgol.coh";
 
# print uint32 right-aligned in column, with
# thousands separators
sub print_col(num: uint32, width: uint8) is
    # maximum integer is 4,294,967,296 for length 13,
    # plus one extra for the zero terminator
    var buf: uint8[14];
    var start := &buf[0];
    var last := UIToA(num, 10, &buf[0]);
 
    # right-align and add separators
    var right := &buf[13];
    var th: uint8 := 3;
    while last >= start loop
        [right] := [last];
        right := @prev right;
        last := @prev last;
        if th == 0 and last >= start then 
            # add separator every 3 characters
            [right] := ',';
            right := @prev right;
            th := 2;
        else
            th := th - 1;
        end if;
    end loop;
 
    # print the result and spaces
    var size := (13 - (right - start)) as uint8;
    while width >= size loop 
        print_char(' '); 
        width := width-1;
    end loop;        
    print(@next right);
end sub;
 
# returns sum of digits of n, given sum of digits of n-1
sub digit_sum(n: uint32, prev: uint32): (sum: uint32) is
    sum := prev + 1;
    while n > 0 and n % 10 == 0 loop
        sum := sum - 9;
        n := n / 10;
    end loop;
end sub;
 
var prev: uint32 := 1;
var gap: uint32 := 0;
var sum: uint32 := 0;
var idx: uint32 := 0;
var gap_idx: uint8 := 1;
var niven: uint32 := 1;
 
print("Gap index  Gap    Niven index    Niven number\n");
while gap_idx <= 27 loop
    sum := digit_sum(niven, sum);
    if (not (sum & 1 == 0 and niven & 1 == 1))
    and (niven % sum == 0) then
        if niven > prev + gap then
            gap := niven - prev;
            print_col(gap_idx as uint32, 9);
            gap_idx := gap_idx + 1;
            print_col(gap, 5);
            print_col(idx, 15);
            print_col(prev, 16);
            print_nl();
        end if;
        prev := niven;
        idx := idx + 1;
    end if;
    niven := niven + 1;
end loop;