%I #37 May 25 2023 13:25:36
%S 2,3,5,7,373,13331,30103,1496941,1970791
%N Primes that are palindromic in bases 8 and 10.
%C Any other terms have more than 20 digits. - _Michael S. Branicky_, Dec 19 2020
%H Patrick De Geest, <a href="http://www.worldofnumbers.com/palpri.htm">World!Of Palindromic Primes</a>
%e 373_10 = 565_8. - _Jon E. Schoenfield_, Apr 10 2021
%t Do[s = RealDigits[n, 8][[1]]; t = RealDigits[n, 10][[1]]; If[PrimeQ[n], If[FromDigits[t] == FromDigits[Reverse[t]], If[FromDigits[s] == FromDigits[Reverse[s]], Print[n]]]], {n, 1, 10^5}]
%t pal810Q[p_]:=PalindromeQ[p]&&IntegerDigits[p,8]==Reverse[IntegerDigits[p,8]]; Select[ Prime[ Range[150000]],pal810Q] (* _Harvey P. Dale_, May 25 2023 *)
%o (Python) # efficiently search to large numbers
%o from sympy import isprime
%o from itertools import product
%o def candidate_prime_pals(digits):
%o ruled_out = "024568" # can't be even or multiple of 5
%o midrange = [[""], "0123456789"]
%o for p in product("0123456789", repeat=digits//2):
%o left = "".join(p)
%o if len(left):
%o if left[0] in ruled_out: continue
%o for middle in midrange[digits%2]:
%o yield left+middle+left[::-1]
%o for digits in range(1, 15):
%o for p in candidate_prime_pals(digits):
%o intp = int(p); octp = oct(intp)[2:]
%o if octp==octp[::-1]:
%o if isprime(intp):
%o print(intp, end=", ") # _Michael S. Branicky_, Dec 19 2020
%o (Python) # alternate sufficient for producing terms through a(9)
%o from sympy import isprime
%o def ispal(n): strn = str(n); return strn==strn[::-1]
%o for n in range(10**7):
%o if ispal(n) and ispal(oct(n)[2:]) and isprime(n):
%o print(n) # _Michael S. Branicky_, Dec 20 2020
%o (PARI) is(n) = my(d=digits(n, 8), dd=digits(n)); d==Vecrev(d) && dd==Vecrev(dd)
%o forprime(p=1, , if(is(p), print1(p, ", "))) \\ _Felix Fröhlich_, Dec 20 2020
%Y Cf. A002113, A002385, A029976, A029804.
%K nonn,hard,base,more
%O 1,1
%A _Patrick De Geest_, Aug 15 1998