OFFSET
1,1
MATHEMATICA
a[1] = 1; a[n_] := a[n] = Block[{b = a[n - 1], p = Prime[n - 1]}, If[ b - p > 0 && Position[t, b - p] == {}, b - p, If[ Position[t, b + p] == {}, b + p, 0]]]; t = {1}; Do[ AppendTo[t, a[n]], {n, 2, 270000}]; Flatten[ Position[t, 0]] (* Robert G. Wilson v, Oct 29 2005 *)
PROG
(Python)
from itertools import count, islice
from sympy import nextprime
def A112877_gen(): # generator of terms
a, aset, p = 1, {1}, 2
for c in count(2):
if (b:=a-p) > 0 and b not in aset:
a = b
elif (b:=a+p) not in aset:
a = b
else:
a = 0
yield c
aset.add(a)
p = nextprime(p)
CROSSREFS
KEYWORD
nonn
AUTHOR
Klaus Brockhaus, Oct 24 2005
EXTENSIONS
a(15) and a(16) from Robert G. Wilson v, Oct 29 2005
a(17) and a(18) from Klaus Brockhaus, Jan 01 2006
a(19)-a(26) from Donovan Johnson, Feb 18 2010
a(27)-a(29) from Chai Wah Wu, Mar 04 2024
a(30)-a(34) from Martin Ehrenstein, Mar 07 2024 (see A370951)
More than the usual number of terms are shown in order to include the new terms from A370951. - N. J. A. Sloane, Mar 10 2024
STATUS
approved