login
Smallest palindromic prime containing the n-th palindrome.
2

%I #18 Jul 11 2024 16:53:22

%S 101,11,2,3,11411,5,10601,7,181,191,11,72227,13331,1444441,15551,

%T 16661,77377,78887,19991,101,1114111,1212121,131,11411,151,1611161,

%U 1117111,181,191,1120211,1212121,72227,32323,12421,3252523,1126211,12721,12821

%N Smallest palindromic prime containing the n-th palindrome.

%H Chai Wah Wu, <a href="/A085054/b085054.txt">Table of n, a(n) for n = 1..10000</a>

%e 1311131 contains the palindrome 111.

%t NextPalindrome[n_] := Block[ {l = Floor[ Log[ 10, n] + 1], idn = IntegerDigits[ n]}, If[ Union[ idn] == {9}, Return[n + 2], If[l < 2, Return[n + 1], If[ FromDigits[ Reverse[ Take[ idn, Ceiling[l/2]]]] FromDigits[ Take[ idn, -Ceiling[l/2]]], FromDigits[ Join[ Take[ idn, Ceiling[l/2]], Reverse[ Take[ idn, Floor[l/2]]]]],

%t idfhn = FromDigits[ Take[ idn, Ceiling[l/2]]] + 1; idp = FromDigits[ Join[ IntegerDigits[ idfhn], Drop[ Reverse[ IntegerDigits[ idfhn]], Mod[l, 2]]]] ]]]]; f[n_] := Block[{k = 0, s = ToString[n]}, While[ !PrimeQ[k] || FromDigits[ Reverse[ IntegerDigits[k]]] != k || StringPosition[ ToString[k], s] == {}, k++ ]; k]; Map[f, NestList[ NextPalindrome, 0, 40]]

%o (Python)

%o from itertools import count

%o from sympy import isprime

%o def A085054(n):

%o s = str((c:=n-x)*x+int(str(c)[-2::-1] or 0) if n<(x:=10**(len(str(n>>1))-1))+(y:=10*x) else (c:=n-y)*y+int(str(c)[::-1] or 0))

%o for k in count(n):

%o p = (c:=k-x)*x+int(str(c)[-2::-1] or 0) if k<(x:=10**(len(str(k>>1))-1))+(y:=10*x) else (c:=k-y)*y+int(str(c)[::-1] or 0)

%o if s in str(p) and isprime(p):

%o return p # _Chai Wah Wu_, Jul 11 2024

%Y Cf. A002113, A083982.

%K base,nonn

%O 1,1

%A _Amarnath Murthy_ and Meenakshi Srikanth (menakan_s(AT)yahoo.com), Jun 26 2003

%E Edited, corrected and extended by _Robert G. Wilson v_, Jun 28 2003