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$?

 A: 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.
A: The number of terms in $x$ rows is $1+2+...+x=x(x+1)/2$. The row number $m$ of $n^{\text{th}}$ term is thus the smallest positive integer solution of $x(x+1)/2\ge n$. The roots of $x^2+x-2n=0$ are $\frac{-1\pm\sqrt{8n+1}}2$. First, we reject the negative root. Next, we take the ceiling value of the positive root as it may not be a whole number.
Thus,$$m=\left\lceil\frac{-1+\sqrt{8n+1}}2\right\rceil$$

As a side note, you can ditch the mod and use regular subtraction in your formula.
