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


 
dim shared as string*16 digits = "0123456789ABCDEF"
 
function get_digit( n as uinteger ) as string
    return mid(digits, n+1, 1)
end function
 
function find_digit( s as string ) as integer
    for i as uinteger = 1 to len(digits)
        if s = mid(digits, i, 1) then return i
    next i
    return -999
end function
 
sub make_base( byval n as uinteger, bse as uinteger, byref ret as string )
    if n = 0 then ret = "0" else ret = ""
    dim as uinteger m
    while n > 0
        m = n mod bse
        ret = mid(digits, m+1, 1) + ret
        n = (n - m)/bse
    wend
end sub
 
function is_esthetic( number as string ) as boolean
    if number = "0" then return false
    if len(number) = 1 then return true
    dim as integer curr = find_digit( left(number,1) ), last, i
    for i = 2 to len(number)
        last = curr
        curr = find_digit( mid(number, i, 1) )
        if abs( last - curr ) <> 1 then return false
    next i
    return true
end function
 
dim as uinteger b, c
dim as ulongint i
dim as string number
for b = 2 to 16
    print "BASE ";b
    i = 0 : c = 0
    while c <= 6*b
        i += 1
        make_base i, b, number
        if is_esthetic( number ) then
            c += 1
            if c >= 4*b then print number;" ";
        end if
    wend
    print 
next b
print "BASE TEN"
for i = 1000 to 9999
    make_base i, 10, number
    if is_esthetic(number) then print number;" ";
next i
print
print "STRETCH GOAL"
for i = 100000000 to 130000000
    make_base i, 10, number
    if is_esthetic(number) then print number;" ";
next i