login
Number of swaps needed to return the recursive transform T(k) = concatenate(reverse(k), len(k) + 1) to the tuple 1, 2, ..., n.
0

%I #27 Sep 25 2021 23:09:11

%S 0,0,1,2,2,4,5,4,6,8,7,10,10,10,13,12,12,14,17,16,18,18,17,22,22,20,

%T 25,24,24,28,29,24,26,32,31,34,32,32,35,38,36,40,35,40,40,40,39,44,46,

%U 42,49,50,44,52,51,52,52,54,51,54,58,56,59,54,54,64,61,60

%N Number of swaps needed to return the recursive transform T(k) = concatenate(reverse(k), len(k) + 1) to the tuple 1, 2, ..., n.

%e For n=7, the T transforms give tuple 6,4,2,1,3,5,7 (triangle A220073 row 7) which requires a(7) = 5 swaps to return to 1,2,3,4,5,6,7.

%o (Python)

%o def swaps(l, start = 0):

%o if len(l) == 0:

%o return 0

%o n = start + 1

%o if l[0] != n:

%o for i, x in enumerate(l):

%o if x == n:

%o l[0], l[i] = l[i], l[0]

%o return 1 + swaps(l[1:], n)

%o else:

%o raise

%o else:

%o return swaps(l[1:], n)

%o def T(l):

%o n = len(l)

%o l.reverse()

%o l.append(n + 1)

%o return l

%o if __name__ == "__main__":

%o l = [ ]

%o for n in range(1, 100):

%o l = T(l)

%o n_swaps = swaps(l[:])

%o print("{}, ".format(n_swaps), end="")

%o (PARI) a(n) = my(h=n>>1); n - #permcycles(vectorsmall(n,i, abs(2*i-n) + (i<=h))); \\ _Kevin Ryde_, Jul 23 2021

%Y Cf. A220073.

%K nonn

%O 1,4

%A _Emanuel Landeholm_, Jul 02 2021