OFFSET
1,1
COMMENTS
Without repeated terms, the primes appear in order as A070865.
Variant of A284172; the difference is that in A284172, a(n+1) = a(n)-i if a(n) is composite and a(1),...,a(n) contains i composites (rather than i primes).
For n >= 3: When a(n) = prime p it is followed by an even number j at a(n+1); p repeats k-j times (where k is the smallest prime > j), appearing at a(n+2m) {m=1..k-j}. a(n+2m+1) = p+m until p+m = k (immediately following the final p); k now becomes "new p" immediately followed by a "new j", and the process repeats.
LINKS
Alois P. Heinz, Table of n, a(n) for n = 1..100000
Michael De Vlieger, Log-log scatterplot of a(n) for n=1..2^16.
Michael De Vlieger, Log-log scatterplot of a(n) for n=1..2^12, showing a(n) in red and A284172(n) in blue, accentuating the first 12 terms of A284172(n) by enlargement.
EXAMPLE
a(10) = 11; there are 7 primes in the sequence up to and including a(10) so a(11) = 11+7 = 18. 18 is composite so a(12) = 18-7 = 11. Now there are 8 primes in the sequence; and since 11 is prime, a(13) = 11+8 = 19 (the 9th prime in the sequence), so a(14) = 28.
MAPLE
c:= proc(n) option remember; `if`(n<1, 0,
`if`(isprime(a(n)), 1, 0)+c(n-1))
end:
a:= proc(n) option remember; `if`(n=1, 2, (m->
`if`(isprime(m), 1, -1)*c(n-1)+m)(a(n-1)))
end:
seq(a(n), n=1..100); # Alois P. Heinz, Apr 15 2017
MATHEMATICA
Block[{c = 1, m = 2, n}, {2}~Join~Reap[Do[If[PrimeQ[m], Set[n, m + c]; c++, Set[n, m - c + 1]]; Sow[n]; m = n, 63]][[-1, -1]]] (* Michael De Vlieger, Oct 20 2021 *)
PROG
(PARI) lista(nn) = {print1(a=2, ", "); nbp = 1; for (n=2, nn, if (isprime(a), a += nbp, a -= nbp); print1(a, ", "); if (isprime(a), nbp++); ); } \\ Michel Marcus, Mar 24 2017
CROSSREFS
KEYWORD
nonn
AUTHOR
Bob Selcoe, Mar 21 2017
STATUS
approved