What is the $n$th term of the sequence $1, 1, 4, 1, 4, 9, 1, 4, 9, 16, 1 ...$ So there exist a lot of similar questions which ask the $n$th term of  
$$1^2 , (1^2 + 2^2) , (1^2 + 2^2 + 3^2) ... $$
Which have a simple answer as
$$T_n = n^3/3 + n^2/2 + n/6$$
But I want to know what will be the $n$th term if we consider each number as a single term. I tried making a pyramid of terms and got this
$1^2\\
1^2~2^2\\
1^2~2^2~3^2\\
1^2~2^2~3^2~4^2\\
...$
I got the $n$th term as$$T_n= (n\mod(m(m-1)/2)^2$$
Where $m$ is the row number.

But now I am stuck, is there a way to eliminate $m$ and write the general term only in terms of $n$?


OEIS has an entry for this sequence. And they give a formula:

a(n) = A000290(m+1), where m = n-t(t+1)/2, t = floor((-1+sqrt(8*n-7))/2)

The sequence named A000290 is just the perfect squares, so A000290(m+1) means $(m+1)^2$. And $m$ is given as $n-\frac{t(t+1)}2$, where $t=\left\lfloor\frac{\sqrt{8n-7}-1}2\right\rfloor$. Nesting out all of this, the finished formula becomes
$$
\left(n-\frac{\left\lfloor\frac{\sqrt{8n-7}-1}2\right\rfloor\left(\left\lfloor\frac{\sqrt{8n-7}-1}2\right\rfloor+1\right)}{2}+1\right)^2
$$
where the brackets indicate the floor function.
