OFFSET
0,3
LINKS
Alois P. Heinz, Table of n, a(n) for n = 0..10
Wikipedia, Pairing function
FORMULA
a(n) = c(n,n-1,...,1) with c() = 0, c(n) = n, c(n,k) = (n+k)*(n+k+1)/2+k, c(n_1,...,n_{k-1},n_k) = c(c(n_1,...,n_{k-1}),n_k) for k>2.
EXAMPLE
a(2) = c(2,1) = 3*4/2+1 = 7.
a(3) = c(3,2,1) = c(c(3,2),1) = c(5*6/2+2,1) = c(17,1) = 18*19/2+1 = 172.
MAPLE
c:= proc() `if`(nargs=0, 0,
`if`(nargs=1, args,
`if`(nargs=2, ((n, k)-> (s-> s*(s+1)/2)(n+k)+k)(args),
c(c(args[1..-2]), args[-1]))))
end:
a:= n-> c((n-i)$i=0..n-1):
seq(a(n), n=0..10);
MATHEMATICA
c[args_List] := Switch[Length[args], 0, {0}, 1, args, 2, {Function[s, s*(s + 1)/2][#[[1]] + #[[2]]] + #[[2]]&[args]}, _, c[Append[c[args[[1 ;; -2]] ], args[[-1]]]]];
a[n_] := c[Table[(n - i), {i, 0, n - 1}]][[1]];
Table[a[n], {n, 0, 10}] (* Jean-François Alcover, May 30 2018, from Maple *)
CROSSREFS
KEYWORD
nonn
AUTHOR
Alois P. Heinz, Jun 13 2013
STATUS
approved