login
a(1)=2; thereafter a(n+1) = a(n)+i if a(n) is a prime and a(1),...,a(n) contains i primes, or a(n+1) = a(n)-i if a(n) is composite and a(1),...,a(n) contains i composite terms.
3

%I #82 Oct 20 2021 21:30:12

%S 2,3,5,8,7,11,16,14,11,17,24,20,15,9,2,10,2,11,21,12,2,13,25,14,2,15,

%T 2,16,2,17,33,18,2,19,37,56,39,21,2,22,2,23,45,24,2,25,2,26,2,27,2,28,

%U 2,29,57,30,2,31,61,92,63,33,2,34,2,35,2,36,2,37,73,110,75,39,2,40,2,41,81,42,2,43,85,44,2,45

%N a(1)=2; thereafter a(n+1) = a(n)+i if a(n) is a prime and a(1),...,a(n) contains i primes, or a(n+1) = a(n)-i if a(n) is composite and a(1),...,a(n) contains i composite terms.

%C Let c(n) be the n-th composite term in the sequence, p(n) the n-th prime term in the sequence.

%C 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.

%C There are three cases:

%C (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.

%C (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.

%C (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)).

%C 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.

%C 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)).

%C From _Bob Selcoe_, Mar 22 2017: (Start)

%C 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:

%C 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.

%C k consecutive primes are followed by k consecutive composites, where k = 1, 2 or 3.

%C 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:

%C 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)

%C From _Benoit Jubin_, Mar 23 2017: (Start)

%C If I'm not mistaken, one has for n > 6,

%C a(2n+1) = 2n+3 if (n+2 is prime or n+1 is a P-prime), and 2 otherwise,

%C a(2n) = 3n+2 if n+1 is a P-prime, and n+2 otherwise,

%C where a P-prime as a prime p such that 2p-1 is also a prime (A005382). (End)

%C _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

%H Charles R Greathouse IV, <a href="/A284172/b284172.txt">Table of n, a(n) for n = 1..10000</a> (first 1000 terms from Luca Petrone)

%H David James Sycamore, <a href="/A284172/a284172.jpg">Graph of first 200 or so terms</a>

%H Michael De Vlieger, <a href="/A284172/a284172.png">Log-log scatterplot of a(n)</a> for n=1..2^12, labeling the first 15 points.

%H Michael De Vlieger, <a href="/A284172/a284172_1.png">Log-log scatterplot of a(n)</a> for n=1..2^24.

%e 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.

%e 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....

%e 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

%p A[1]:= 2: P:= 0;

%p for n from 1 to 999 do

%p if isprime(A[n]) then

%p P:= P+1;

%p A[n+1]:= A[n]+P;

%p elif A[n] <> 1 then

%p A[n+1]:= A[n]-(n-P);

%p fi

%p od:

%p [seq(A[i],i=1..1000)]; # _Robert Israel_, Mar 21 2017

%t 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_ *)

%t (* Second program can generate 2^24 terms in 32 s: *)

%t 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 *)

%o (Python)

%o from sympy import isprime

%o a=[2]

%o p=0

%o for n in range(1, 1000):

%o if isprime(a[n - 1]):

%o p+=1

%o a.append(a[n - 1] + p)

%o else:

%o a.append(a[n - 1] - n + p)

%o print(a) # _Indranil Ghosh_, Mar 22 2017

%o (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

%Y Cf. A005382, A284188, A284278.

%K nonn

%O 1,1

%A _David James Sycamore_, Mar 21 2017