An esthetic number is a positive integer where every adjacent digit differs from its neighbour by 1.



E.G.
 12 is an esthetic number. One and two differ by 1.
 5654 is an esthetic number. Each digit is exactly 1 away from its neighbour.
 890 is not an esthetic number. Nine and zero differ by 9.


These examples are nominally in base 10 but the concept extends easily to numbers in other bases. Traditionally, single digit numbers are included in esthetic numbers; zero may or may not be. For our purposes, for this task, do not include zero (0) as an esthetic number. Do not include numbers with leading zeros.

Esthetic numbers are also sometimes referred to as stepping numbers.



Task
 Write a routine (function, procedure, whatever) to find esthetic numbers in a given base.
 Use that routine to find esthetic numbers in bases 2 through 16 and display, here on this page, the esthectic numbers from index (base × 4) through index (base × 6), inclusive. (E.G. for base 2: 8th through 12th, for base 6: 24th through 36th, etc.)
 Find and display, here on this page, the base 10 esthetic numbers with a magnitude between 1000 and 9999.
 Stretch: Find and display, here on this page, the base 10 esthetic numbers with a magnitude between 1.0e8 and 1.3e8.


Related task

   numbers with equal rises and falls


See also

OEIS A033075 - Positive numbers n such that all pairs of consecutive decimal digits differ by 1
Numbers Aplenty - Esthetic numbers
Geeks for Geeks - Stepping numbers


\ Returns the next esthetic number in the given base after n, where n is an
\ esthetic number in that base or one less than a power of base.
: next_esthetic_number { n base -- n }
  n 1+ base < if n 1+ exit then
  n base / dup base mod
  dup n base mod 1+ = if dup 1+ base < if 2drop n 2 + exit then then
  drop base recurse
  dup base mod
  dup 0= if 1+ else 1- then
  swap base * + ;
 
: print_esthetic_numbers { min max per_line -- }
  ." Esthetic numbers in base 10 between " min 1 .r ."  and " max 1 .r ." :" cr
  0
  min 1- 10 next_esthetic_number
  begin
    dup max <=
  while
    dup 4 .r
    swap 1+ dup per_line mod 0= if cr else space then swap
    10 next_esthetic_number
  repeat
  drop
  cr ." count: " . cr ;
 
: main
  17 2 do
    i 4 * i 6 * { min max }
    ." Esthetic numbers in base " i 1 .r ."  from index " min 1 .r ."  through index " max 1 .r ." :" cr
    0
    max 1+ 1 do
      j next_esthetic_number
      i min >= if dup ['] . j base-execute then
    loop
    drop
    cr cr
  loop
  1000 9999 16 print_esthetic_numbers cr
  100000000 130000000 8 print_esthetic_numbers ;
 
main
bye