OFFSET
1,1
COMMENTS
Extending a number by inserting a prepending "0" is obviously not allowed. Smallness of digit has precedence over rightmostness of position. If no prime extension exists, the sequence terminates.
Sequence construction very similar to A356557 (the difference arises from the order of the conditions).
Length of a(n) is n.
Is the sequence infinite?
Sequence terminates if and only if it contains a term of A125001.
LINKS
Michael S. Branicky, Table of n, a(n) for n = 1..1000
EXAMPLE
a(2) = 23 because the numbers 20, 21, 12, 22 obtained from a(1) = 2 are composite and 23 is a prime.
For n=6, starting from a(5)=22003 and appending a digit "0" from right to left gives 220030, 220003, 202003, which are not primes. Inserting a digit "1" from right to left gives 220031 which also is not prime, and 220013 which is prime, so a(6) = 220013.
MAPLE
f:= proc(n) local x, y, z, j;
for x from 0 to 9 do
for j from 0 to length(n)-`if`(x=0, 1, 0) do
y:= n mod 10^j;
z:= y + x*10^j + 10*(n-y);
if isprime(z) then return z fi;
od od;
FAIL
end proc:
R:= 2: x:= 2:
for count from 2 to 30 while x <> FAIL do
x:= f(x); R:= R, x;
od:
R; # Robert Israel, Sep 29 2022
MATHEMATICA
a[1]=2; a[n_] := a[n] = Catch@ Block[{p, d = IntegerDigits[a[n-1]]}, Do[p = FromDigits@ Insert[d, c, -i]; If[p > a[n - 1] && PrimeQ[p], Throw@p], {c, 0, 9}, {i, 1 + Length@ d}]]; Array[a, 22] (* Giovanni Resta, Oct 13 2022 *)
PROG
(Python)
from sympy import isprime
from itertools import islice
def anext(an):
s = str(an)
for c in "0123456789":
for k in range(len(s)+1):
w = s + c if k == 0 else s[:-k] + c + s[-k:]
if w[0] != "0" and isprime(int(w)): return int(w)
def agen(an=2):
while an != None: yield an; an = anext(an)
print(list(islice(agen(), 22))) # Michael S. Branicky, Sep 29 2022
CROSSREFS
KEYWORD
nonn,base
AUTHOR
Bartlomiej Pawlik, Sep 28 2022
EXTENSIONS
More terms from Robert Israel, Sep 29 2022
STATUS
approved