3
$\begingroup$

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.

$\endgroup$
  • 3
    $\begingroup$ Look up ReplaceAll. Alternatively, just use Root[]. $\endgroup$ – J. M. will be back soon Jun 29 '15 at 12:32
  • $\begingroup$ How would I use that here? What am I looking to replace? $\endgroup$ – A is for Ambition Jun 29 '15 at 12:33
  • $\begingroup$ You noticed that the output of your function is a Rule, yes? What's on the left? $\endgroup$ – J. M. will be back soon Jun 29 '15 at 12:35
  • $\begingroup$ A curly bracket. Sorry, I'm trying but failing to see how ReplaceAll works. $\endgroup$ – A is for Ambition Jun 29 '15 at 12:36
  • 1
    $\begingroup$ I'm lazy: x /. nthtri[28] . $\endgroup$ – Eric Towers Jun 29 '15 at 17:42
3
$\begingroup$

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.

$\endgroup$
3
$\begingroup$

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}]
$\endgroup$
  • 1
    $\begingroup$ By any chance, did this involve simplifying the + case for the quadratic formula? $\endgroup$ – A is for Ambition Jun 29 '15 at 12:48
  • 1
    $\begingroup$ That would be it, yes. $\endgroup$ – J. M. will be back soon Jun 29 '15 at 12:50
  • 1
    $\begingroup$ Yes you can do: Solve[k == Expand[\!\( \*UnderoverscriptBox[\(\[Sum]\), \(i = 1\), \(n\)]i\)], n] $\endgroup$ – Enrique Pérez Herrero Jun 29 '15 at 12:52
2
$\begingroup$

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}]}]]

enter image description here

$\endgroup$
1
$\begingroup$

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;
$\endgroup$
  • $\begingroup$ Welcome to Mathematica.SE! I hope you will become a regular contributor. To get started, 1) take the introductory Tour now, 2) when you see good questions and answers, vote them up by clicking the gray triangles, because the credibility of the system is based on the reputation gained by users sharing their knowledge, 3) remember to accept the answer, if any, that solves your problem, by clicking the checkmark sign, and 4) give help too, by answering questions in your areas of expertise. $\endgroup$ – bbgodfrey Jun 29 '15 at 14:06
  • $\begingroup$ Please edit your answer to display your solution in Mathematica format. $\endgroup$ – bbgodfrey Jun 29 '15 at 14:07

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.