# How to Find "n" for n-th Triangular Number

For example, the 7th triangular number is 28. How do I create something that will tell me that 28 is the 7th triangular number?

This is what I have created.

nthtri := Part[Solve[x^2 + x - (2 #) == 0, x], 2] &


This is what happens when executed.

nthtri[28]


returns

{x -> 7}


This newly defined function solves for the two roots of the quadratic equation. Since Mathematica lists the roots in increasing order, and since there are only two roots (one negative, one positive), by taking the 2nd root, you will find the "n" for a particular triangular number.

I think my current method is unprofessional and inelegant. How do I create a function that will return a singular output value? For example, I would like

nthtri[28]


to return

7


Edit: As suggested below by @Guesswhoitis, using Root works.

nthtri := Root[x^2 + x - (2 #), 2] &


But I am still looking for more solution methods. I got lucky that solving for triangular numbers involves a quadratic equation that strictly has one positive and one negative root. If it were in a different case, for instance with a cubic formula where no such root signs are guaranteed, Root would not be able to consistently generate correct answers.

• Look up ReplaceAll. Alternatively, just use Root[]. Jun 29, 2015 at 12:32
• How would I use that here? What am I looking to replace? Jun 29, 2015 at 12:33
• You noticed that the output of your function is a Rule, yes? What's on the left? Jun 29, 2015 at 12:35
• A curly bracket. Sorry, I'm trying but failing to see how ReplaceAll works. Jun 29, 2015 at 12:36
• I'm lazy: x /. nthtri[28] . Jun 29, 2015 at 17:42

## 4 Answers

Following the comment and your own implementation with Root it is better to define this:

nthtri[t_] := Root[#^2 + # - 2 t &, 2]

nthtri[28]

7


This has no unlocalized Symbols as your own version had. With your code if you set x = 5 before using it it will fail.

f is the function you are looking for:

f[k_] := (1/2)*(-1 + Sqrt[1 + 8*k]);
t[n_] := (1/2)*n*(1 + n);
Table[f[t[k]], {k, 1, 10}]

• By any chance, did this involve simplifying the + case for the quadratic formula? Jun 29, 2015 at 12:48
• That would be it, yes. Jun 29, 2015 at 12:50
• Yes you can do: Solve[k == Expand[\!$$\*UnderoverscriptBox[\(\[Sum]$$, $$i = 1$$, $$n$$]i\)], n] Jun 29, 2015 at 12:52

Why not replace # with n and take second part as your function?

(x /. Solve[x (x + 1)/2 == n, x])[[2]]

(*1/2 (-1 + Sqrt[1 + 8 n])*)

f[n_] := 1/2 (-1 + Sqrt[1 + 8 n])

f@28

(*7*)

With[{nn = 4}, Plot[f@x, {x, 0, # (# + 1)/2 &@nn}, Epilog -> {Red,
PointSize[Medium], Point[Transpose@{# (# + 1)/2 & /@ Range@nn, Range@nn}]}]]


If $t, n$ are the triangular number and its position, then you want $n(n+1) = 2t$

Completing the square

$n^2 + n + \frac{1}{4} = 2t + \frac{1}{4}$

and solving for positive n:

$n = \sqrt{2t + \frac{1}{4}} - \frac{1}{2}$

nthtri[t_] := Sqrt[2*t + 0.25] - 0.5;

• Please edit your answer to display your solution in Mathematica format. Jun 29, 2015 at 14:07