# Largest Triangular Number less than a Given Natural Number

I want to determine the closest Triangular number a particular natural number is. For example, the first 10 triangular numbers are $1,3,6,10,15,21,28,36,45,55$ and thus, the number $57$ can be written as $$57=T_{10}+2$$ The number $54$ can be written as $$54=T_{9}+9\neq T_{10}-1$$ The second part highlights that I am looking for Triangular numbers larger than a particular positive, and not necessarily "closer" in terms of distance from Triangular numbers.

My approach would be this; given the $n$-th Triangular number has the formula $$T_n=\frac{n(n+1)}{2}=\binom{n+1}{2}$$ If I'm looking for a particular breakdown and close Triangular number, my number, say, $M$ will be of the form $$M=T_k+r$$ where $0\le r\le k$, and thus $$2(M-r)=k^2+k$$ And thus $$k=\frac{-1\pm\sqrt{1+8(M-r)}}{2}$$ I'm lost here in trying to solve, given that $r$ varies. I know that $r$ is less than or equal to $k$ but for sufficiently large $M$, how would I go about finishing solving?

$\binom{k}{2}\leq n$ is equivalent to $(2k-1)^2 \leq 8n+1$, hence the largest triangular number $\leq n$ is given by $\binom{k}{2}$ with: $$k = \left\lfloor \frac{1+\sqrt{8n+1}}{2}\right\rfloor.$$
• That was easier than I anticipated...should have known that you could floor out the $r$. Thanks! – Lalaloopsy Sep 1 '15 at 22:07
• I think it should be $\binom{k+1}{2}\le n$ is equivalent to $(2k+1)^2\le 8n+1$... – Lalaloopsy Sep 1 '15 at 23:31
• @Lalaloopsy: it is exactly the same, you are just replacing my $k$ with your $k+1$. – Jack D'Aurizio Sep 2 '15 at 0:05
• But then if say, $n=4, k=3$. Since I defined $T_k=\binom{k+1}{2}$, this would mean that $T_3=6\le 4$... – Lalaloopsy Sep 2 '15 at 0:09