Find palindromic numbers in both binary and ternary bases

You are encouraged to solve this task according to the task description, using any language you may know.
Task

   Find and show (in decimal) the first six numbers (non-negative integers) that are   palindromes   in   both:
   base 2
   base 3
   Display   0   (zero) as the first number found, even though some other definitions ignore it.
   Optionally, show the decimal number found in its binary and ternary form.
   Show all output here.


It's permissible to assume the first two numbers and simply list them.



See also
   Sequence A60792,   numbers that are palindromic in bases 2 and 3 on The On-Line Encyclopedia of Integer Sequences.


with javascript_semantics
-- widths and limits for 32/64 bit running (see output below):
constant {dsize,w3,w2,limit} = iff(machine_bits()=32?{12,23,37,6}
                                                    :{18,37,59,7}),
-- [atoms on 32-bit have only 53 bits of precision, but 7th ^^^^ requires 59]
         dfmt = sprintf("%%%dd",dsize), -- ie "%12d" or "%18d"
         esc = #1B
 
function center(string s, integer l)
    l = max(0,floor((l-length(s))/2))
    string space = repeat(' ',l)
    s = space & s & space
    return s
end function
 
integer count = 1
 
procedure show(atom n, string p2, p3)
    if count=1 then
        printf(1,"    %s %s %s\n",{pad_head("decimal",dsize),center("ternary",w3),center(" binary",w2)})
    end if
    string ns = sprintf(dfmt,n)
    printf(1,"%2d: %s %s %s\n",{count, ns, center(p3,w3), center(p2,w2)})
    count += 1
end procedure
 
procedure progress64(string e, p2, p3)
    e = pad_head(e,dsize)
    printf(1,"--: %s %s %s\r",{e, center(p3,w3), center(p2,w2)})
end procedure
 
function to_base(atom i, integer base)
    string s = ""
    while i>0 do
        s = append(s,remainder(i,base)+'0')
        i = floor(i/base)
    end while
    s = reverse(s)
    if s="" then
        s = "0"
    end if
    return s
end function
 
function from_base(string s, integer base)
    atom res = 0
    for i=1 to length(s) do
        res = res*base+s[i]-'0'
    end for
    return res
end function
 
function sn(string s, integer f, base)
-- helper function, return s mirrored (if f!=0) 
--       and as a (decimal) number (if base!=0)
-- all returns from next_palindrome() get fed through here.
    if f then
        s[f+2..$] = reverse(s[1..f])
    end if
    atom n = iff(base?from_base(s,base):0)
    return {s,n}
end function
 
function next_palindrome(integer base, object s)
--
-- base is 2 or 3
-- s is not usually a palindrome, but derived from one in <5-base>
--
-- all done with very obvious string manipulations, plus a few
-- less obvious optimisations (odd length, middle 1 in base 3).
--
-- example: next_palindrome(2,"10001000100") -> "10001010001"
--
    if not string(s) then s = to_base(s,base) end if
    integer l = length(s), 
            f = floor(l/2), 
            m = f+1, c
    if mod(l,2) then    -- optimisation: palindromes must be odd-length
        -- 1) is a plain mirror greater? (as in the example just given)
        {string r} = sn(s,f,0)
        -- optimisation: base 3 palindromes have '1' in the middle
        if base=3 and r[m]!='1' then r[m] = '1' end if
        if r>s then return sn(r,0,base) end if
        -- 2) can we (just) increment the middle digit?
        c = s[m]-'0'+1
        if base=2 or c=1 then
            if c<base then
                s[m] = c+'0'
                return sn(s,f,base)
            end if
            s[m] = '0'
        elsif base=3 then
            s[m] = '1'
        end if
        -- 3) can we increment left half (or is it all <base-1>s?)
        for i=f to 1 by -1 do
            if s[i]<base-1+'0' then
                s[i] += 1
                return sn(s,f,base)
            else
                s[i] = '0'
            end if
        end for
        l += 2  -- (stay odd)
    else
        l += 1  -- (even->odd)
    end if
    -- 4) well then, next palindrome is longer, 1000..0001-style
    s = sprintf("1%s1",{repeat('0',l-2)})
    -- optimisation: base 3 palindromes have '1' in the middle
    if base=3 then
        m = (l+1)/2
        s[m] = '1'
    end if
    return sn(s,0,base)
end function
 
string p2 = "0", p3 = "0"   -- palindromes as strings in base 2 and 3
atom n2 = 0, n3 = 0,        -- decimal equivalents of the above.
     t0 = time(),
     t1 = time()+1
 
while count<=limit do
    if n2=n3 then
        show(n2,p2,p3)
        {p2,n2} = next_palindrome(2,p2)
        {p3,n3} = next_palindrome(3,p3)
    elsif n2<n3 then
        {p2,n2} = next_palindrome(2,n3-1)
    elsif n2>n3 then
        {p3,n3} = next_palindrome(3,n2-1)
    end if
    if time()>t1 and platform()!=JS then
        progress64(elapsed_short(time()-t0),p2,p3)
        t1 = time()+1
        if find(get_key(),{'q','Q',esc}) then exit end if
    end if
end while
?elapsed(time()-t0)
