OFFSET
1,2
COMMENTS
Permutation of the natural numbers.
a(n) is a pairing function: a function that reversibly maps Z^{+} x Z^{+} onto Z^{+}, where Z^{+} is the set of integer positive numbers.
Layer is pair of sides of square from T(1,n) to T(n,n) and from T(n,n) to T(n,1). Enumeration table T(n,k) is layer by layer. The order of the list:
T(1,1)=1;
T(2,2), T(1,2), T(2,1);
...
T(n,n), T(n-1,n), T(n,n-1), ... T(1,n), T(n,1);
...
LINKS
Boris Putievskiy, Rows n = 1..140 of triangle, flattened
Boris Putievskiy, Transformations [of] Integer Sequences And Pairing Functions arXiv:1212.2732 [math.CO], 2012.
Eric W. Weisstein, MathWorld: Pairing functions
FORMULA
As a table,
T(n,k) = n*n - 2*k + 2, if n >= k;
T(n,k) = k*k - 2*n + 1, if n < k.
As a linear sequence,
a(n) = i*i - 2*j + 2, if i >= j;
a(n) = j*j - 2*i + 1, if i < j
where
i = n - t*(t+1)/2,
j = (t*t + 3*t + 4)/2 - n,
t = floor((-1 + sqrt(8*n-7))/2).
EXAMPLE
The start of the sequence as a table:
1, 3, 8, 15, 24, 35, ...
4, 2, 6, 13, 22, 33, ...
9, 7, 5, 11, 20, 31, ...
16, 14, 12, 10, 18, 29, ...
25, 23, 21, 19, 17, 27, ...
36, 34, 32, 30, 28, 26, ...
...
The start of the sequence as triangular array read by rows:
1;
3, 4;
8, 2, 9;
15, 6, 7, 16;
24, 13, 5, 14, 25;
35, 22, 11, 12, 23, 36;
...
MATHEMATICA
f[n_, k_] := n^2 - 2*k + 2 /; n >= k; f[n_, k_] := k^2 - 2*n + 1 /; n < k; TableForm[Table[f[n, k], {n, 1, 5}, {k, 1, 10}]]; Table[f[n - k + 1, k], {n, 5}, {k, n, 1, -1}] // Flatten (* G. C. Greubel, Aug 19 2017 *)
PROG
(Python)
t=int((math.sqrt(8*n-7) - 1)/ 2)
i=n-t*(t+1)/2
j=(t*t+3*t+4)/2-n
if i >= j:
result=i*i-2*j+2
else:
result=j*j-2*i+1
CROSSREFS
KEYWORD
nonn,tabl
AUTHOR
Boris Putievskiy, Mar 05 2013
STATUS
approved