When a number is written in base 10,   adjacent digits may "rise" or "fall" as the number is read   (usually from left to right).



Definition

Given the decimal digits of the number are written as a series   d:

   A   rise   is an index   i   such that    d(i)  <  d(i+1)
   A   fall    is an index   i   such that    d(i)  >  d(i+1)


Examples

   The number   726,169   has   3   rises and   2   falls,   so it isn't in the sequence.
   The number     83,548   has   2   rises and   2   falls,   so it   is   in the sequence.


Task

   Print the first   200   numbers in the sequence 
   Show that the   10 millionth   (10,000,000th)   number in the sequence is   41,909,002


See also

   OEIS Sequence  A296712   describes numbers whose digit sequence in base 10 have equal "rises" and "falls".


Related tasks

   Esthetic numbers


include "cowgol.coh";
 
# return the change in height of a number
sub height(n: uint32): (h: int8) is
    h := 0;
    var dgt := (n % 10) as uint8;
    var prev: uint8;
    n := n / 10;
 
    while n > 0 loop    
        prev := dgt;
        dgt := (n % 10) as uint8;
        n := n / 10;
        if prev < dgt then
            h := h + 1;
        elseif prev > dgt then
            h := h - 1;
        end if;
    end loop;
end sub;
 
var number: uint32 := 0; 
var seen: uint32 := 0; 
var col: uint8 := 10;
 
print("The first 200 numbers are:");
print_nl();
while seen < 10000000 loop
    loop
        number := number + 1;
        if height(number) == 0 then break; end if;
    end loop;
    seen := seen + 1;
    if seen <= 200 then
        print_i32(number);
        col := col - 1;
        if col != 0 then
            print_char('\t');
        else
            print_char('\n');
            col := 10;
        end if;
    end if;
end loop;
 
print_nl();
print("The 10,000,000th number is: ");
print_i32(number);
print_nl();