A284172 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.
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, 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, 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
Offset: 1
Keywords
Examples
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
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.
Programs
-
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 *)
-
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
-
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
Comments