How to find nth tribonacci number with matrix multiplication method if the initial values are some arbitrary numbers say 1, 2 3 i.e T(1) = 1, T(2) =2 and T(3) = 3.
If T(n) = T(n-1) + T(n-2) + T(n-3) then how to find T(n) if n is very very large, I would appreciate if anyone can explain with matrix multiplication method. How to construct initial matrix.
It is also generalized to higher-order sequences includ- ing tribonacci[5], quatranacci, k-step Fibonacci sequences[1]. The 3-step Fibonacci sequence usually called the tribonacci sequence Tn is the sum of the preceding three terms having initial values 0,0,1.
A generalized Fibonacci sequence can start with any two numbers and then apply the rule that subsequent terms are defined as the sum of their two predecessors. For example, if we start with 3 and 4, we get the sequence 3, 4, 7, 11, 18, 29, …
The Tribonacci constant η is the one real root of the cubic: x3−x2−x−1=0. Its decimal expansion starts: η=1⋅83928675521416…
The matrix multiplication method involves using the matrix recurrence relation.
For the Fibonacci series, we can define a vector of length 2 to represent adjacent Fibonacci numbers. Using this vector, we can define a recurrence relation with a matrix multiplication:
Similarly, the Tribonacci series recurrence relation can be written in this way:
The only difference is that the vector and matrix sizes are different.
Now, to calculate a large Tribonacci number, we just apply the matrix multiplication n times, and we get:
The matrix to the power of n
(Mn) can be efficiently calculated, because we can use an exponentiation algorithm.
Many efficient exponentiation algorithms for scalars are described by Wikipedia in Exponentiation by Squaring. We can use the same idea for matrix exponentiation.
I will describe a simple way to do this. First we write n
as a binary number, eg:
n = 37 = 100101
Then, calculate M to each power of 2 by squaring the previous power of 2: M1, M2 = M1M1, M4 = M2M2, M8 = M4M4, M16 = M8M8, M32 = M16M16, ...
And finally, multiply the powers of M corresponding to the binary digits of n
. In this case, Mn = M1M4M32.
After calculating that, we can multiply the matrix with the Tribonacci vector for the first 3 values, ie.
Because the matrices have fixed size, each matrix multiplication takes constant time. We must do O(log n)
matrix multiplications. Thus, we can calculate the nth Tribonacci number in O(log n)
time.
Compare this to the normal dynamic programming method, where it takes O(n)
time, by calculating each Tribonacci number up to the nth Tribonacci number (ie. for (i = 3 to n) {T[i] = T[i-1]+T[i-2]+T[i-3];} return T[n];
).
I will assume that you know how to code up matrix multiplication in the language of your choice.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With