%I #14 Sep 03 2024 15:03:45
%S 1,2,6,3,12,4,10,5,35,7,14,8,18,9,33,11,143,13,39,15,20,16,34,17,323,
%T 19,57,21,24,22,46,23,115,25,30,26,36,27,42,28,58,29,899,31,62,32,74,
%U 37,148,38,40,82,41,1763,43,86,44,48,45,141,47,329,49,56,50
%N A variant of the EKG sequence (A064413) where the least value not yet in the sequence appears as soon as possible.
%C To build the sequence:
%C - we start with a(1) = 1 and a(2) = 2, and then repeatedly:
%C - let a(n) be the last known term and v the least value not yet in the sequence,
%C - if gcd(a(n), v) > 1
%C then a(n+1) = v,
%C - otherwise:
%C - let w be the least value not yet in the sequence such that gcd(a(n), w) > 1
%C and gcd(w, v) > 1,
%C - then a(n+1) = w and a(n+2) = v.
%C This sequence is a permutation of the positive integers with inverse A355213.
%C The construction is similar to that of A352713.
%H Rémy Sigrist, <a href="/A355212/b355212.txt">Table of n, a(n) for n = 1..10000</a>
%H Rémy Sigrist, <a href="/A355212/a355212.gp.txt">PARI program</a>
%H <a href="/index/Ed#EKG">Index entries for sequences related to EKG sequence</a>
%H <a href="/index/Per#IntegerPermutation">Index entries for sequences that are permutations of the natural numbers</a>
%e The first terms are (stars correspond to "w" terms):
%e n a(n) w
%e -- ---- -
%e 1 1
%e 2 2
%e 3 6 *
%e 4 3
%e 5 12 *
%e 6 4
%e 7 10 *
%e 8 5
%e 9 35 *
%e 10 7
%e 11 14 *
%e 12 8
%e 13 18 *
%e 14 9
%e 15 33 *
%e 16 11
%o (PARI) \\ See Links section.
%o (Python)
%o from math import gcd
%o from itertools import count, islice
%o def agen(): # generator of terms
%o aset, an, v = {1, 2}, 2, 3; yield from [1, 2]
%o for n in count(3):
%o if gcd(an, v) == 1:
%o w = v + 1
%o while w in aset or gcd(an, w) == 1 or gcd(w, v) == 1: w += 1
%o aset.add(w); yield w
%o an = v; aset.add(an); yield an
%o while v in aset: v += 1
%o print(list(islice(agen(), 65))) # _Michael S. Branicky_, Jun 24 2022
%Y Cf. A064413, A352713, A355213 (inverse).
%K nonn
%O 1,2
%A _Rémy Sigrist_, Jun 24 2022