## Tribonacci Numbers

### September 14, 2012

You will recall that fibonacci numbers are formed by a sequence starting with 0 and 1 where each succeeding number is the sum of the two preceeding numbers; that is, F[n] = F[n-1] + F[n-2] with F[0] = 0 and F[1] = 1. We studied fibonacci numbers in a previous exercise.

Tribonacci numbers are like fibonacci numbers except that the starting sequence is 0, 0 and 1 and each succeeding number is the sum of the three preceeding numbers; that is, T[n] = T[n-1] + T[n-2] + T[n-3] with T[-1] = 0, T[0] = 0 and T[1] = 1. The powering matrix for tribonacci numbers, used similarly to the powering matrix for fibonacci numbers, is:

The first ten terms of the tribonacci sequence, ignoring the two leading zeros, are 1, 1, 2, 4, 7, 13, 24, 44, 81 and 149 (A000073).

Your task is to write two functions that calculate the first *n* terms of the tribonacci sequence by iteration and the *n*th term by matrix powering; you should also calculate the tribonacci constant, which is the limit of the ratio between successive tribonacci numbers as *n* tends to infinity. When you are finished, you are welcome to read or run a suggested solution, or to post your own solution or discuss the exercise in the comments below.

A python version that uses the numpy library for matrices

Another Python version with the power of a matrix explicitly in the code.

Calculation of the limit of the ratio as one of the roots the characteristic polynomial.

This is also the real eigenvalue of the powering matrix.

It’s been a while since I’ve used GNU Octave (Matlab clone), so I thought I’d give it a try. Not particularly clever, but it was good practice to brush up on working with matrices.

My implementation in Common lisp

As I mentioned in a recent comment on the fibonacci post, it’s actually impossible to achieve O(log n) performance for arbitrarily large n. The sequence is (to a close approximation) exponential, so the lengths of the binary or decimal values increase linearly. It is thus impossible for any algorithm to run in sublinear time.

Another Python solution:

#include

using namespace std;

int main()

{

unsigned long int Counter;

unsigned long int result = 0;

unsigned long int numa, numb, numc;

numa = 0;

numb = 0;

numc = 1;

unsigned long int number;

cout << "Qual o numero que desejas conhecer da sequencia tribonacci?" << endl <> number;

cout << endl << endl;

for(Counter = 0; Counter < number – 1; Counter++)

{

result = numa + numb + numc;

numa = numb;

numb = numc;

numc = result;

}

cout << "O Valor Tribonacci do numero " << number << " e igual a: " << result;

return 0;

}