login
a(0) = 0, and for any n > 0, a(n) is the least integer (in absolute value) not yet in the sequence such that the absolute difference of a(n-1) and a(n) is a prime number; in case of a tie, preference is given to the positive value.
6

%I #24 Jan 23 2025 12:38:15

%S 0,2,-1,1,-2,3,-4,-6,-3,4,6,-5,-7,-9,8,5,7,9,-8,-10,-12,11,13,10,12,

%T -11,-13,-15,14,16,18,15,-14,-16,-18,19,17,20,-17,-19,-21,22,24,21,

%U -20,-22,-24,23,25,27,-26,-23,-25,-27,26,28,30,-29,-31,-28,-30,29

%N a(0) = 0, and for any n > 0, a(n) is the least integer (in absolute value) not yet in the sequence such that the absolute difference of a(n-1) and a(n) is a prime number; in case of a tie, preference is given to the positive value.

%C This sequence is a variant of A277618 allowing negative values.

%C Will every integer appear in the sequence?

%H Paolo Xausa, <a href="/A377090/b377090.txt">Table of n, a(n) for n = 0..10000</a> (terms 0..1000 from Rémy Sigrist).

%H Rémy Sigrist, <a href="/A377090/a377090.png">Scatterplot of the first 100000 terms of the partial sums</a>

%H Rémy Sigrist, <a href="/A377090/a377090.gp.txt">PARI program</a>

%e The first terms are:

%e n a(n) |a(n)-a(n-1)|

%e -- ---- -------------

%e 0 0 N/A

%e 1 2 2

%e 2 -1 3

%e 3 1 2

%e 4 -2 3

%e 5 3 5

%e 6 -4 7

%e 7 -6 2

%e 8 -3 3

%e 9 4 7

%e 10 6 2

%e 11 -5 11

%e 12 -7 2

%e 13 -9 2

%e 14 8 17

%t A377090list[nmax_] := Module[{s, a = 0, an, u = 1}, s[_] := False; s[0] = True; Join[{0}, Table[a = (While[s[u] && s[-u], u++]; an = u; While[s[an] || !PrimeQ[Abs[a - an]], an = Boole[an < 0] - an]; s[an] = True; an), nmax]]];

%t A377090list[100] (* _Paolo Xausa_, Jan 20 2025 *)

%o (PARI) \\ See Links section.

%o (Python)

%o from sympy import isprime

%o from itertools import count, islice

%o def cond(n): return isprime(n)

%o def agen(): # generator of terms

%o an, aset, m = 0, {0}, 1

%o for n in count(0):

%o yield an

%o an = next(s for k in count(m) for s in [k, -k] if s not in aset and cond(abs(an-s)))

%o aset.add(an)

%o while m in aset and -m in aset: m += 1

%o print(list(islice(agen(), 62))) # _Michael S. Branicky_, Dec 27 2024

%Y Cf. A277618, A377091, A377092, A380311 (partial sums).

%K sign,look

%O 0,2

%A _Rémy Sigrist_, Oct 16 2024