OFFSET
1,1
COMMENTS
Let c(n) be the n-th composite term in the sequence, p(n) the n-th prime term in the sequence.
After a(14) the sequence returns repeatedly to its start term 2, at each re-occurrence of which the pattern of terms is: n+1, 2, n+2, ... or more general1y c(n-1), p(n), a(2*n), a(2*n+1), a(2*n+2), ... where c(n-1) = n+1, p(n) = 2, a(2*n) = n+2. What happens next depends on whether a(2*n) = n+2 is prime or not.
There are three cases:
(1) a(2*n) = n+2 is composite; so = c(n), a(2*n+1) = n+2-n = 2 = p(n+1). Then a(2*n+2) = 2+n+1 = n+3. The pattern (n+1),2,(n+2),2,(n+3),...continues for as long as the term following 2 remains composite.
(2) a(2*n) = n+2 is prime; therefore it is p(n+1), and n must be odd (since n+2 odd). Therefore a(2*n+1) = n+2+n+1g2*n+3. If this term is composite then it is c(n), so a(2*n+2) = 2*n+3-n = n+3 which is even because n is odd, and therefore it is c(n+1), so then a(2*n+3) = (n+3)-(n+1) = 2 = p(n+2), thus the pattern is (n+1),2,(n+2),(2*n+3),(n+3),2 and so on.
(3) a(2*n) = n+2 = p(n+1) and a(2*n+1) = 2*n+3 = p(n+2) are both prime. Then n is odd (as in case 2) and a(2*n+2) = 2*n+3 + n+2 = 3*n+5 (even since n is odd), so is c(n). Therefore a(2*n+3) = 3*n+5-n = 2*n+5 (divisible by 3 since n is odd and 3*n+5 is even), so is c(n+1), (hence maximum number of consecutive primes is three). Therefore a(2*n+4) = 2*n+5-(n+1) = n+4; divisible by 3, hence is c(n+2), so a(2*n+5) = (n+4)-(n+2) = 2 and the sequence continues as (1) or (2). (Note in case (3) the term n+3 is missing, but is present in (1) and (2)).
The sequence returns to its starting point at a(15) = 2, then oscillates (see graph) between positions bounded by four distinct straight lines, one of which is the base line y = 2, and the other three, corresponding to spikes, are stacked above it, and all meet at y = 2. Spikes on the upper two lines are generated by the appearances of primes appearing together in groups of two or three consecutive terms respectively (more than three consecutive primes is impossible). Every term after a(14), whether spike or not, lies on one or other of these four lines (and all terms before that, except for a(7) and a(11) also lie on these lines.
A fifth straight line, also passing through y = 2 is suggested at the start of the sequence, by a(7) and a(11) but it seems that this, being a line above all the others, plays no further part in locating points of the sequence, because this would require the generation of four consecutive primes, which is impossible (see case (3)).
From Bob Selcoe, Mar 22 2017: (Start)
Because a(14) = 9 is composite, c(n)=p(n) = 7 (the first time c(n)=p(n)) and 9-7 = 2, the following observations hold for n > 14:
2's appear exclusively at odd indices (true for all n, since only a(1) = 2 when n <= 14); all other even terms appear exclusively at even indices.
k consecutive primes are followed by k consecutive composites, where k = 1, 2 or 3.
2 always follows a composite c, and repeats every other term, interleaved with c+1, c+2, ..., S, where S is the smallest prime > c; so S is any prime immediately following 2. Since p(n) = S-1 when a(n) = S, then a(n+1) = 2S-1 when a(n) = S. The sequence therefore can be constructed as follows, given a(15) = 2 and a(16) = 10:
Let a(n) = S, a prime. If 2S-1 is composite, then a(n+1) = 2S-1, a(n+2) = S+1 and a(n+3) = 2; if 2S-1 is prime, then a(n+1) = 2S-1, a(n+2) = 3S-1, a(n+3) = 2S+1, a(n+4) = S+2 and a(n+5) = 2. (End)
From Benoit Jubin, Mar 23 2017: (Start)
If I'm not mistaken, one has for n > 6,
a(2n+1) = 2n+3 if (n+2 is prime or n+1 is a P-prime), and 2 otherwise,
a(2n) = 3n+2 if n+1 is a P-prime, and n+2 otherwise,
where a P-prime as a prime p such that 2p-1 is also a prime (A005382). (End)
Benoit Jubin's comment implies of course that for n>6, the points lie on 4 straight lines, of slopes 0 (when a(n)=2), 1/2, 1, and 3/2. N. J. A. Sloane, Mar 23 2017
LINKS
Charles R Greathouse IV, Table of n, a(n) for n = 1..10000 (first 1000 terms from Luca Petrone)
David James Sycamore, Graph of first 200 or so terms
Michael De Vlieger, Log-log scatterplot of a(n) for n=1..2^12, labeling the first 15 points.
Michael De Vlieger, Log-log scatterplot of a(n) for n=1..2^24.
EXAMPLE
a(1)=2, the first prime to occur, so a(2)=2+1=3, 2nd prime to appear, so a(3)=3+2=5, third prime so a(4)=5+3=8, first composite we have seen.
So a(5)=8-1=7, fourth prime so a(6)=7+4=11, fifth prime so a(7)=11+5=16, second composite so a(8)=16-2=14, and so on....
From the Mar 22 2017 comments: c(16) = 10 (composite), so a(17) = 2, a(18) = 11 = S; a(19) = 2S-1 = 21 (composite), so a(20) = S+1 = 12 and a(21) = 2. Now since 2 follows 12, a(22) = 13 = S, followed by 25, 14, 2, 15, 2, 16, 2, 17 = S, 33, 18, etc. - Bob Selcoe, Mar 22 2017
MAPLE
A[1]:= 2: P:= 0;
for n from 1 to 999 do
if isprime(A[n]) then
P:= P+1;
A[n+1]:= A[n]+P;
elif A[n] <> 1 then
A[n+1]:= A[n]-(n-P);
fi
od:
[seq(A[i], i=1..1000)]; # Robert Israel, Mar 21 2017
MATHEMATICA
a = {2}; n = 1; i = 1; j = 1; For[n = 1, n < 1000, n++, If[PrimeQ[a[[n]]], AppendTo[a, a[[n]] + i]; i++, AppendTo[a, a[[n]] - j]; j++]] (* Luca Petrone *)
(* Second program can generate 2^24 terms in 32 s: *)
Block[{c = 1, m = 2, n}, {2}~Join~Reap[Do[If[PrimeQ[m], Set[n, m + c]; c++, Set[n, m - i + c - 1]]; Sow[n]; m = n, {i, 85}]][[-1, -1]]] (* Michael De Vlieger, Oct 20 2021 *)
PROG
(Python)
from sympy import isprime
a=[2]
p=0
for n in range(1, 1000):
if isprime(a[n - 1]):
p+=1
a.append(a[n - 1] + p)
else:
a.append(a[n - 1] - n + p)
print(a) # Indranil Ghosh, Mar 22 2017
(PARI) first(n)=my(v=vector(n), p, c); v[1]=2; for(k=2, n, v[k]=if(isprime(v[k-1]), p++, -c++)+v[k-1]); v \\ Charles R Greathouse IV, Mar 22 2017
CROSSREFS
KEYWORD
nonn
AUTHOR
David James Sycamore, Mar 21 2017
STATUS
approved