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


: in-seq? ( n -- is N in the sequence? )
  0 swap            \ height 
  10 /mod           \  digit and rest of number 
  begin dup while   \ as long as the number isn't zero... 
    10 /mod         \ get next digit and quotient
    -rot swap       \ retrieve previous digit 
    over - sgn      \ see if higher, lower or equal (-1, 0, 1) 
    >r rot r> +     \ add to height
    -rot swap       \ quotient on top of stack 
  repeat
  drop drop         \ drop number and last digit 
  0=                \ is height equal to zero? 
;
 
: next-val ( n -- n: retrieve first element of sequence higher than N )
  begin 1+ dup in-seq? until
;
 
: two-hundred
  begin over 200 < while 
    next-val dup . 
    swap 1+ swap
  repeat  
;
 
: ten-million
  begin over 10000000 < while 
    next-val
    swap 1+ swap
  repeat
;
 
0 0 \ top of stack: current index and number 
." The first 200 numbers are: " two-hundred cr cr 
." The 10,000,000th number is: " ten-million . cr
bye