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


/*REXX pgm  finds and displays  N  numbers that have an equal number of rises and falls,*/
parse arg n .                                    /*obtain optional argument from the CL.*/
if n=='' | n==","  then n= 200                   /*Not specified?  Then use the default.*/
append= n>0                                      /*a flag that is used to append numbers*/
n= abs(n)                                        /*use the absolute value of  N.        */
call init                                        /*initialize the  rise/fall  database. */
          do j=1  until #==n                     /*test integers until we have N of them*/
          s= 0                                   /*initialize the sum of  rises/falls.  */
                        do k=1  for length(j)-1  /*obtain a set of two digs from number.*/
                        t= substr(j, k, 2)       /*obtain a set of two digs from number.*/
                        s= s + @.t               /*sum the rises and falls in the number*/
                        end   /*k*/
          if s\==0  then iterate                 /*Equal # of rises & falls? Then add it*/
          #= # + 1                               /*bump the count of numbers found.     */
          if append  then $= $ j                 /*append to the list of numbers found. */
          end   /*j*/
 
if append  then call show                        /*display a list of  N  numbers──►term.*/
           else say  'the '  commas(n)th(n)  " number is: "   commas(j)    /*show Nth #.*/
exit 0                                           /*stick a fork in it,  we're all done. */
/*──────────────────────────────────────────────────────────────────────────────────────*/
commas:  parse arg _;  do c=length(_)-3  to 1  by -3; _=insert(',', _, c); end;   return _
th:      parse arg th;  return word('th st nd rd',1+(th//10)*(th//100%10\==1)*(th//10<4))
/*──────────────────────────────────────────────────────────────────────────────────────*/
init: @.= 0;   do i=1  for 9;    _= i' ';     @._= 1;    _= '0'i;   @._= +1;   end  /*i*/
               do i=10  to 99;   parse var i  a 2 b;     if a>b  then              @.i= -1
                                                                 else if a<b  then @.i= +1
               end   /*i*/;      #= 0;        $=;        return
/*──────────────────────────────────────────────────────────────────────────────────────*/
show: say 'the first '   commas(#)   " numbers are:";   say;       w= length( word($, #) )
      _=;    do o=1  for n;     _= _ right( word($, o), w);    if o//20\==0  then iterate
             say substr(_, 2);  _=               /*display a line;  nullify a new line. */
             end   /*o*/                         /* [↑]  display  20  numbers to a line.*/
 
      if _\==''  then say substr(_, 2);   return /*handle any residual numbers in list. */