# List of Tribonacci Polynomials with Mathematica? [duplicate]

I want to list top ten of Tribonacci polynomials. I have following algorithm, but it doesnt work.

Tribonacci[0] := 0
Tribonacci[1] := 1
Tribonacci[2] := x^2
Tribonacci[3] := x^4 + x
Tribonacci[n_] := Tribonacci[n] =
x^2 Tribonacci[n - 1] + x*Tribonacci[n - 2] +  Tribonacci[n - 3]

Tribonacci /@ Range[5]

(* Out:= {1, x^2, x + x^4, 1 + x^3 + x^2 (x + x^4),
x^2 + x (x + x^4) + x^2 (1 + x^3 + x^2 (x + x^4))} *)

-

## marked as duplicate by Artes, whuber, m_goldberg, Sjoerd C. de Vries, Michael E2May 17 '13 at 1:11

Here's one way to define the series:

Clear[f];
f[n_] := f[n - 1] + f[n - 2] + f[n - 3];
f[1] = 1; f[2] = 1; f[3] = 1;


Now you can get any f

f[10]


gives the answer 105. Similarly, if you want to define the polynomials, you could set up a recursion

Clear[g];
g[n_] := x^2 g[n - 1] + x g[n - 2] + g[n - 3];
g[0] = 0; g[1] = 1; g[2] = x^2;


Now you can find the nth g by

 g[5]


which gives

 x^2 + x (x + x^4) + x^2 (1 + x^3 + x^2 (x + x^4))


which of course can be simplified using Expand[g[5]] or FullSimplify[g[5]] depending which you think is simpler. Apply to find the first 10 by g/@Range[10]

If this is going too slowly (for large n), you could add the memoization trick, which is very simple to do: instead of f[n] above, you could use

    f[n_] := f[n] = f[n - 1] + f[n - 2] + f[n - 3];


and now the lower numbers are cached (and hence subsequent calls are quicker). Same thing can be done with g[n].

-

Using a technique discussed in this answer, here's how you might define the "tribonacci" numbers/polynomials recursively:

SetAttributes[Tribonacci, Listable];
Tribonacci[0, x_] := 0;
Tribonacci[1, x_] := 1;
Tribonacci[2, x_] := x^2;
Tribonacci[n_Integer, x_] := Module[{al, xl},
Set @@ Hold[Tribonacci[n, xl_],
Expand[xl^2 Tribonacci[n - 1, xl] + xl Tribonacci[n - 2, xl] + Tribonacci[n - 3, xl]]];
Tribonacci[n, x]];
Tribonacci[n_Integer] := Tribonacci[n, 1]


Since I set the function to be Listable, it is now easy to generate a pile of these polynomials:

Tribonacci[Range[0, 10], t]
{0, 1, t^2, t + t^4, 1 + 2 t^3 + t^6, 3 t^2 + 3 t^5 + t^8, 2 t + 6 t^4 + 4 t^7 + t^10,
1 + 7 t^3 + 10 t^6 + 5 t^9 + t^12, 6 t^2 + 16 t^5 + 15 t^8 + 6 t^11 + t^14,
3 t + 19 t^4 + 30 t^7 + 21 t^10 + 7 t^13 + t^16,
1 + 16 t^3 + 45 t^6 + 50 t^9 + 28 t^12 + 8 t^15 + t^18}


Alternatively, you can use LinearRecurrence[] if you just want to generate the pile directly:

LinearRecurrence[{t^2, t, 1}, {0, 1, t^2}, 11] // Expand


Another alternative is to use DifferenceRoot[] in the definition of Tribonacci[]:

Tribonacci[n_Integer, x_] :=
DifferenceRoot[Function[{y, k}, {y[k] == x^2 y[k - 1] + x y[k - 2] + y[k - 3],
y[0] == 0, y[1] == 1, y[2] == x^2}]][n]

-
See this article if you want more information on $n$-nacci polynomials in general. –  Ｊ. Ｍ. May 16 '13 at 15:22
Clear[t];
t[n_] := x^2*t[n - 1] + x*t[n - 2] + t[n - 3];
t[0] := 0; t[1] := 1; t[2] := x^2; t[3] := x^4 + x;


The above equations are the definition of the Tribonacci polynomials.

To print some elements of Tribonacci sequence;

Table[Expand[t[i]], {i, 0, 5}]

-
You skipped t[3]. Also, you should space out you definitions a bit more for readability; putting them on separate lines, generally, works well. –  rcollyer May 16 '13 at 13:56
@rcollyer, you were right. I changed it. –  MATIRMAK May 16 '13 at 14:00
Incidentally, the grave marks (  ) only perform inline code formatting. To put into block form, precede each line with four spaces, and don't wrap it in the grave marks. Look at my revisions to see what I mean. –  rcollyer May 16 '13 at 14:10
@rcollyer, you don't need to specify t[3]... it can be calculated from the previous three terms. –  bill s May 16 '13 at 15:31
@bills true, but he had f[4] := x^4 + x`. Of course, he's missing a few terms ... –  rcollyer May 16 '13 at 18:51
show 1 more comment