%I #33 Jun 15 2024 09:25:49
%S 0,1,2,11,22,101,111,121,202,212,222,1001,1111,1221,2002,2112,2222,
%T 10001,10101,10201,11011,11111,11211,12021,12121,12221,20002,20102,
%U 20202,21012,21112,21212,22022,22122,22222,100001,101101,102201,110011,111111,112211,120021
%N Palindromes in base 3 (written in base 3).
%C The number of n-digit terms is given by A225367. - _M. F. Hasler_, May 05 2013 [Moved here on May 08 2013]
%C Digit-wise application of A000578 (and also superposition of a(n) with its horizontal OR vertical reflection) yields A006072. - _M. F. Hasler_, May 08 2013
%C Equivalently, palindromes k (written in base 10) such that 4*k is a palindrome. - _Bruno Berselli_, Sep 12 2018
%H Michael S. Branicky, <a href="/A118594/b118594.txt">Table of n, a(n) for n = 1..10000</a>
%H Eric Weisstein's World of Mathematics, <a href="http://mathworld.wolfram.com/PalindromicNumber.html">Palindromic Number</a>
%H Eric Weisstein's World of Mathematics, <a href="http://mathworld.wolfram.com/Ternary.html">Ternary</a>
%t (* get NextPalindrome from A029965 *) Select[NestList[NextPalindrome, 0, 1110], Max@IntegerDigits@# < 3 &] (* _Robert G. Wilson v_, May 09 2006 *)
%t Select[FromDigits/@Tuples[{0,1,2},8],IntegerDigits[#]==Reverse[ IntegerDigits[ #]]&] (* _Harvey P. Dale_, Apr 20 2015 *)
%o (PARI) {for(l=1,5,u=vector((l+1)\2,i,10^(i-1)+(2*i-1<l)*10^(l-i))~;forvec(v=vector((l+1)\2,i,[l>1&&i==1,2]), print1(v*u",")))} \\ The n-th term could be produced by using (partial sums of) A225367 to skip all shorter terms, and then skipping the adequate number of vectors v until n is reached. - _M. F. Hasler_, May 08 2013
%o (Sage)
%o [int(n.str(base=3)) for n in (0..757) if Word(n.digits(3)).is_palindrome()] # _Peter Luschny_, Sep 13 2018
%o (Python)
%o from itertools import count, islice, product
%o def agen(): # generator of terms
%o yield from [0, 1, 2]
%o for d in count(2):
%o for start in "12":
%o for rest in product("012", repeat=d//2-1):
%o left = start + "".join(rest)
%o for mid in [[""], ["0", "1", "2"]][d%2]:
%o yield int(left + mid + left[::-1])
%o print(list(islice(agen(), 42))) # _Michael S. Branicky_, Mar 29 2022
%o (Python)
%o from sympy import integer_log
%o from gmpy2 import digits
%o def A118594(n):
%o if n == 1: return 0
%o y = 3*(x:=3**integer_log(n>>1,3)[0])
%o return int((s:=digits(n-x,3))+s[-2::-1] if n<x+y else (s:=digits(n-y,3))+s[::-1]) # _Chai Wah Wu_, Jun 14 2024
%Y Cf. A007089, A014190, A057148, A118595, A118596, A118597, A118598, A118599, A118600, A002113.
%K nonn,base,easy
%O 1,3
%A _Martin Renner_, May 08 2006
%E More terms from _Robert G. Wilson v_, May 09 2006
%E a(40) and beyond from _Michael S. Branicky_, Mar 29 2022