Context
Consider square matrices with n
columns and rows containing the first n^2
(i.e. n
squared) positive integers, where n
is odd. The elements of the matrices are arranged such that the integers 1
through n^2
are placed sequentially in a counterclockwise spiral starting at the center and initially moving to the left. Call these matrices M(n)
For n=1
this simply gives the one element matrix M(1)=[[1]]
.
M(3)
is the matrix
9 8 7
2 1 6
3 4 5
M(5)
is the matrix
25 24 23 22 21
10 9 8 7 20
11 2 1 6 19
12 3 4 5 18
13 14 15 16 17
and M(7)
is the matrix
49 48 47 46 45 44 43
26 25 24 23 22 21 42
27 10 9 8 7 20 41
28 11 2 1 6 19 40
29 12 3 4 5 18 39
30 13 14 15 16 17 38
31 32 33 34 35 36 37
Now consider flattening this matrix into a list/array by concatenating its rows starting from the top and moving down. Call these lists L(n)
. L(3)
, L(5)
and L(7)
are represented below, with their elements delimited by spaces.
9 8 7 2 1 6 3 4 5 (n=3)
25 24 23 22 21 10 9 8 7 20 11 2 1 6 19 12 3 4 5 18 13 14 15 16 17 (n=5)
49 48 47 46 45 44 43 26 25 24 23 22 21 42 27 10 9 8 7 20 41 28 11 2 1 6 19 40 29 12 3 4 5 18 39 30 13 14 15 16 17 38 31 32 33 34 35 36 37 (n=7)
We can find the index i(n)
of L(n)
in a lexicographically sorted list of permutations of L(n)
. In Jelly, the Œ¿
atom gives this index for the list it acts on.
Challenge
Your challenge is to take an positive odd integer n
as input and output the index i(n)
.
The first few values are
n i(n)
-------
1 1
3 362299
5 15511208759089364438087641
7 608281864033718930841258106553056047013696596030153750700912081
Note that i(n)
~= (n^2)!
. This is not on OEIS.
This is code golf per language, so achieve this in the fewest bytes possible.