The tribonacci series is a generalization of the Fibonacci sequence where each term is the sum of the three preceding terms.
The Tribonacci Sequence:
0, 0, 1, 1, 2, 4, 7, 13, 24, 44, 81, 149, 274, 504, 927, 1705, 3136, 5768, 10609, 19513, 35890, 66012, 121415, 223317, 410744, 755476, 1389537, 2555757, 4700770, 8646064, 15902591, 29249425, 53798080, 98950096, 181997601, 334745777, 615693474, 1132436852… so on
General Form of Tribonacci number:
a(n) = a(n-1) + a(n-2) + a(n-3)
with
a(0) = a(1) = 0, a(2) = 1.
Given a value N, task is to print first N Tribonacci Numbers.
Examples:
Input : 5
Output : 0, 0, 1, 1, 2
Input : 10
Output : 0, 0, 1, 1, 2, 4, 7, 13, 24, 44
Input : 20
Output : 0, 0, 1, 1, 2, 4, 7, 13, 24, 44,
81, 149, 274, 504, 927, 1705, 3136,
5768, 10609, 19513
A simple solution is to simply follow recursive formula and write recursive code for it,
C++
#include <iostream>
using namespace std;
int printTribRec( int n)
{
if (n == 0 || n == 1 || n == 2)
return 0;
if (n == 3)
return 1;
else
return printTribRec(n - 1) +
printTribRec(n - 2) +
printTribRec(n - 3);
}
void printTrib( int n)
{
for ( int i = 1; i < n; i++)
cout << printTribRec(i) << " " ;
}
int main()
{
int n = 10;
printTrib(n);
return 0;
}
|
Java
import java.io.*;
class GFG {
static int printTribRec( int n)
{
if (n == 0 || n == 1 || n == 2 )
return 0 ;
if (n == 3 )
return 1 ;
else
return printTribRec(n - 1 ) +
printTribRec(n - 2 ) +
printTribRec(n - 3 );
}
static void printTrib( int n)
{
for ( int i = 1 ; i < n; i++)
System.out.print(printTribRec(i)
+ " " );
}
public static void main(String args[])
{
int n = 10 ;
printTrib(n);
}
}
|
Python
def printTribRec(n) :
if (n = = 0 or n = = 1 or n = = 2 ) :
return 0
elif (n = = 3 ) :
return 1
else :
return (printTribRec(n - 1 ) +
printTribRec(n - 2 ) +
printTribRec(n - 3 ))
def printTrib(n) :
for i in range ( 1 , n) :
print ( printTribRec(i) , " " , end = "")
n = 10
printTrib(n)
|
C#
using System;
class GFG {
static int printTribRec( int n)
{
if (n == 0 || n == 1 || n == 2)
return 0;
if (n == 3)
return 1;
else
return printTribRec(n - 1) +
printTribRec(n - 2) +
printTribRec(n - 3);
}
static void printTrib( int n)
{
for ( int i = 1; i < n; i++)
Console.Write(printTribRec(i)
+ " " );
}
public static void Main()
{
int n = 10;
printTrib(n);
}
}
|
PHP
<?php
function printTribRec( $n )
{
if ( $n == 0 || $n == 1 || $n == 2)
return 0;
if ( $n == 3)
return 1;
else
return printTribRec( $n - 1) +
printTribRec( $n - 2) +
printTribRec( $n - 3);
}
function printTrib( $n )
{
for ( $i = 1; $i <= $n ; $i ++)
echo printTribRec( $i ), " " ;
}
$n = 10;
printTrib( $n );
?>
|
Javascript
<script>
function printTribRec(n)
{
if (n == 0 || n == 1 || n == 2)
return 0;
if (n == 3)
return 1;
else
return printTribRec(n - 1) +
printTribRec(n - 2) +
printTribRec(n - 3);
}
function printTrib(n)
{
for (let i = 1; i <= n; i++)
document.write(printTribRec(i) + " " );
}
let n = 10;
printTrib(n);
</script>
|
Output
0 0 1 1 2 4 7 13 24
Time complexity of above solution is exponential.
A better solution is to use Dynamic Programming.
1) Top-Down Dp Memoization:
C++
#include <bits/stdc++.h>
using namespace std;
int printTribRec( int n, vector< int > &dp)
{
if (n == 0 || n == 1 || n == 2)
return 0;
if (dp[n] != -1){
return dp[n] ;
}
if (n == 3)
return 1;
else
return dp[n] = printTribRec(n - 1, dp) +
printTribRec(n - 2, dp) +
printTribRec(n - 3, dp);
}
void printTrib( int n)
{
vector< int > dp(n+1, -1) ;
for ( int i = 1; i < n; i++)
cout << printTribRec(i, dp) << " " ;
}
int main()
{
int n = 10;
printTrib(n);
return 0;
}
|
Python3
def tribonacci(n):
h = {}
def tribo(n):
if n in h:
return h[n]
if n = = 0 :
return 0
elif n = = 1 or n = = 2 :
return 1
else :
res = tribo(n - 3 ) + tribo(n - 2 ) + tribo(n - 1 )
h[n] = res
return res
return tribo(n)
n = 10
for i in range (n):
print (tribonacci(i),end = ' ' )
|
Javascript
function printTribRec(n, dp)
{
if (n == 0 || n == 1 || n == 2)
return 0;
if (dp[n] != -1){
return dp[n] ;
}
if (n == 3)
return 1;
else
return dp[n] = printTribRec(n - 1, dp) +
printTribRec(n - 2, dp) +
printTribRec(n - 3, dp);
}
function printTrib(n)
{
let dp = new Array(n+1).fill(-1) ;
for ( var i = 1; i < n; i++)
process.stdout.write(printTribRec(i, dp) + " " );
}
let n = 10;
printTrib(n);
|
C#
using System;
using System.Collections.Generic;
class GFG {
static int printTribRec( int n, List< int > dp)
{
if (n == 0 || n == 1 || n == 2)
return 0;
if (dp[n] != -1) {
return dp[n];
}
if (n == 3)
return 1;
else
return dp[n] = printTribRec(n - 1, dp)
+ printTribRec(n - 2, dp)
+ printTribRec(n - 3, dp);
}
static void printTrib( int n)
{
List< int > dp = new List< int >();
for ( var i = 0; i <= n; i++)
dp.Add(-1);
for ( int i = 1; i < n; i++)
Console.Write(printTribRec(i, dp) + " " );
}
public static void Main( string [] args)
{
int n = 10;
printTrib(n);
}
}
|
Java
import java.util.*;
class GFG {
static int printTribRec( int n, int [] dp)
{
if (n == 0 || n == 1 || n == 2 )
return 0 ;
if (dp[n] != - 1 ) {
return dp[n];
}
if (n == 3 )
return 1 ;
else
return dp[n] = printTribRec(n - 1 , dp)
+ printTribRec(n - 2 , dp)
+ printTribRec(n - 3 , dp);
}
static void printTrib( int n)
{
int [] dp = new int [n + 1 ];
for (var i = 0 ; i <= n; i++)
dp[i] = - 1 ;
for ( int i = 1 ; i < n; i++)
System.out.print(printTribRec(i, dp) + " " );
}
public static void main(String[] args)
{
int n = 10 ;
printTrib(n);
}
}
|
Output
0 0 1 1 2 4 7 13 24
2) Bottom-Up DP Tabulation:
C++
#include <iostream>
using namespace std;
int printTrib( int n)
{
int dp[n];
dp[0] = dp[1] = 0;
dp[2] = 1;
for ( int i = 3; i < n; i++)
dp[i] = dp[i - 1] +
dp[i - 2] +
dp[i - 3];
for ( int i = 0; i < n; i++)
cout << dp[i] << " " ;
}
int main()
{
int n = 10;
printTrib(n);
return 0;
}
|
Java
import java.io.*;
class GFG {
static void printTrib( int n)
{
int dp[]= new int [n];
dp[ 0 ] = dp[ 1 ] = 0 ;
dp[ 2 ] = 1 ;
for ( int i = 3 ; i < n; i++)
dp[i] = dp[i - 1 ] +
dp[i - 2 ] +
dp[i - 3 ];
for ( int i = 0 ; i < n; i++)
System.out.print(dp[i] + " " );
}
public static void main(String args[])
{
int n = 10 ;
printTrib(n);
}
}
|
Python3
def printTrib(n) :
dp = [ 0 ] * n
dp[ 0 ] = dp[ 1 ] = 0 ;
dp[ 2 ] = 1 ;
for i in range ( 3 ,n) :
dp[i] = dp[i - 1 ] + dp[i - 2 ] + dp[i - 3 ];
for i in range ( 0 ,n) :
print (dp[i] , " " , end = "")
n = 10
printTrib(n)
|
C#
using System;
class GFG {
static void printTrib( int n)
{
int []dp = new int [n];
dp[0] = dp[1] = 0;
dp[2] = 1;
for ( int i = 3; i < n; i++)
dp[i] = dp[i - 1] +
dp[i - 2] +
dp[i - 3];
for ( int i = 0; i < n; i++)
Console.Write(dp[i] + " " );
}
public static void Main()
{
int n = 10;
printTrib(n);
}
}
|
PHP
<?php
function printTrib( $n )
{
$dp [0] = $dp [1] = 0;
$dp [2] = 1;
for ( $i = 3; $i < $n ; $i ++)
$dp [ $i ] = $dp [ $i - 1] +
$dp [ $i - 2] +
$dp [ $i - 3];
for ( $i = 0; $i < $n ; $i ++)
echo $dp [ $i ] , " " ;
}
$n = 10;
printTrib( $n );
?>
|
Javascript
<script>
function printTrib(n)
{
let dp = Array.from({length: n}, (_, i) => 0);
dp[0] = dp[1] = 0;
dp[2] = 1;
for (let i = 3; i < n; i++)
dp[i] = dp[i - 1] +
dp[i - 2] +
dp[i - 3];
for (let i = 0; i < n; i++)
document.write(dp[i] + " " );
}
let n = 10;
printTrib(n);
</script>
|
Output
0 0 1 1 2 4 7 13 24 44
Time complexity of above is linear, but it requires extra space. We can optimizes space used in above solution using three variables to keep track of previous three numbers.
C++
#include <iostream>
using namespace std;
void printTrib( int n)
{
if (n < 1)
return ;
int first = 0, second = 0;
int third = 1;
cout << first << " " ;
if (n > 1)
cout << second << " " ;
if (n > 2)
cout << second << " " ;
for ( int i = 3; i < n; i++)
{
int curr = first + second + third;
first = second;
second = third;
third = curr;
cout << curr << " " ;
}
}
int main()
{
int n = 10;
printTrib(n);
return 0;
}
|
Java
import java.io.*;
class GFG {
static void printTrib( int n)
{
if (n < 1 )
return ;
int first = 0 , second = 0 ;
int third = 1 ;
System.out.print(first + " " );
if (n > 1 )
System.out.print(second + " " );
if (n > 2 )
System.out.print(second + " " );
for ( int i = 3 ; i < n; i++)
{
int curr = first + second + third;
first = second;
second = third;
third = curr;
System.out.print(curr + " " );
}
}
public static void main(String args[])
{
int n = 10 ;
printTrib(n);
}
}
|
Python3
def printTrib(n) :
if (n < 1 ) :
return
first = 0
second = 0
third = 1
print ( first , " " , end = "")
if (n > 1 ) :
print (second, " " ,end = "")
if (n > 2 ) :
print (second, " " , end = "")
for i in range ( 3 , n) :
curr = first + second + third
first = second
second = third
third = curr
print (curr , " " , end = "")
n = 10
printTrib(n)
|
C#
using System;
class GFG {
static void printTrib( int n)
{
if (n < 1)
return ;
int first = 0, second = 0;
int third = 1;
Console.Write(first + " " );
if (n > 1)
Console.Write(second + " " );
if (n > 2)
Console.Write(second + " " );
for ( int i = 3; i < n; i++)
{
int curr = first + second + third;
first = second;
second = third;
third = curr;
Console.Write(curr + " " );
}
}
public static void Main()
{
int n = 10;
printTrib(n);
}
}
|
PHP
<?php
function printTrib( $n )
{
if ( $n < 1)
return ;
$first = 0; $second = 0;
$third = 1;
echo $first , " " ;
if ( $n > 1)
echo $second , " " ;
if ( $n > 2)
echo $second , " " ;
for ( $i = 3; $i < $n ; $i ++)
{
$curr = $first + $second + $third ;
$first = $second ;
$second = $third ;
$third = $curr ;
echo $curr , " " ;
}
}
$n = 10;
printTrib( $n );
?>
|
Javascript
<script>
function printTrib(n)
{
if (n < 1)
return ;
let first = 0, second = 0;
let third = 1;
document.write(first + " " );
if (n > 1)
document.write(second + " " );
if (n > 2)
document.write(second + " " );
for (let i = 3; i < n; i++)
{
let curr = first + second + third;
first = second;
second = third;
third = curr;
document.write(curr + " " );
}
}
let n = 10;
printTrib(n);
</script>
|
Output
0 0 0 1 2 4 7 13 24 44
Below is more efficient solution using matrix exponentiation.
C++
#include <iostream>
using namespace std;
void multiply( int T[3][3], int M[3][3])
{
int a, b, c, d, e, f, g, h, i;
a = T[0][0] * M[0][0] +
T[0][1] * M[1][0] +
T[0][2] * M[2][0];
b = T[0][0] * M[0][1] +
T[0][1] * M[1][1] +
T[0][2] * M[2][1];
c = T[0][0] * M[0][2] +
T[0][1] * M[1][2] +
T[0][2] * M[2][2];
d = T[1][0] * M[0][0] +
T[1][1] * M[1][0] +
T[1][2] * M[2][0];
e = T[1][0] * M[0][1] +
T[1][1] * M[1][1] +
T[1][2] * M[2][1];
f = T[1][0] * M[0][2] +
T[1][1] * M[1][2] +
T[1][2] * M[2][2];
g = T[2][0] * M[0][0] +
T[2][1] * M[1][0] +
T[2][2] * M[2][0];
h = T[2][0] * M[0][1] +
T[2][1] * M[1][1] +
T[2][2] * M[2][1];
i = T[2][0] * M[0][2] +
T[2][1] * M[1][2] +
T[2][2] * M[2][2];
T[0][0] = a;
T[0][1] = b;
T[0][2] = c;
T[1][0] = d;
T[1][1] = e;
T[1][2] = f;
T[2][0] = g;
T[2][1] = h;
T[2][2] = i;
}
void power( int T[3][3], int n)
{
if (n == 0 || n == 1)
return ;
int M[3][3] = {{ 1, 1, 1 },
{ 1, 0, 0 },
{ 0, 1, 0 }};
power(T, n / 2);
multiply(T, T);
if (n % 2)
multiply(T, M);
}
int tribonacci( int n)
{
int T[3][3] = {{ 1, 1, 1 },
{ 1, 0, 0 },
{ 0, 1, 0 }};
if (n == 0 || n == 1)
return 0;
else
power(T, n - 2);
return T[0][0];
}
int main()
{
int n = 10;
for ( int i = 0; i < n; i++)
cout << tribonacci(i) << " " ;
cout << endl;
return 0;
}
|
Java
import java.io.*;
class GFG
{
static void multiply( int T[][], int M[][])
{
int a, b, c, d, e, f, g, h, i;
a = T[ 0 ][ 0 ] * M[ 0 ][ 0 ] +
T[ 0 ][ 1 ] * M[ 1 ][ 0 ] +
T[ 0 ][ 2 ] * M[ 2 ][ 0 ];
b = T[ 0 ][ 0 ] * M[ 0 ][ 1 ] +
T[ 0 ][ 1 ] * M[ 1 ][ 1 ] +
T[ 0 ][ 2 ] * M[ 2 ][ 1 ];
c = T[ 0 ][ 0 ] * M[ 0 ][ 2 ] +
T[ 0 ][ 1 ] * M[ 1 ][ 2 ] +
T[ 0 ][ 2 ] * M[ 2 ][ 2 ];
d = T[ 1 ][ 0 ] * M[ 0 ][ 0 ] +
T[ 1 ][ 1 ] * M[ 1 ][ 0 ] +
T[ 1 ][ 2 ] * M[ 2 ][ 0 ];
e = T[ 1 ][ 0 ] * M[ 0 ][ 1 ] +
T[ 1 ][ 1 ] * M[ 1 ][ 1 ] +
T[ 1 ][ 2 ] * M[ 2 ][ 1 ];
f = T[ 1 ][ 0 ] * M[ 0 ][ 2 ] +
T[ 1 ][ 1 ] * M[ 1 ][ 2 ] +
T[ 1 ][ 2 ] * M[ 2 ][ 2 ];
g = T[ 2 ][ 0 ] * M[ 0 ][ 0 ] +
T[ 2 ][ 1 ] * M[ 1 ][ 0 ] +
T[ 2 ][ 2 ] * M[ 2 ][ 0 ];
h = T[ 2 ][ 0 ] * M[ 0 ][ 1 ] +
T[ 2 ][ 1 ] * M[ 1 ][ 1 ] +
T[ 2 ][ 2 ] * M[ 2 ][ 1 ];
i = T[ 2 ][ 0 ] * M[ 0 ][ 2 ] +
T[ 2 ][ 1 ] * M[ 1 ][ 2 ] +
T[ 2 ][ 2 ] * M[ 2 ][ 2 ];
T[ 0 ][ 0 ] = a;
T[ 0 ][ 1 ] = b;
T[ 0 ][ 2 ] = c;
T[ 1 ][ 0 ] = d;
T[ 1 ][ 1 ] = e;
T[ 1 ][ 2 ] = f;
T[ 2 ][ 0 ] = g;
T[ 2 ][ 1 ] = h;
T[ 2 ][ 2 ] = i;
}
static void power( int T[][], int n)
{
if (n == 0 || n == 1 )
return ;
int M[][] = {{ 1 , 1 , 1 },
{ 1 , 0 , 0 },
{ 0 , 1 , 0 }};
power(T, n / 2 );
multiply(T, T);
if (n % 2 != 0 )
multiply(T, M);
}
static int tribonacci( int n)
{
int T[][] = {{ 1 , 1 , 1 },
{ 1 , 0 , 0 },
{ 0 , 1 , 0 }};
if (n == 0 || n == 1 )
return 0 ;
else
power(T, n - 2 );
return T[ 0 ][ 0 ];
}
public static void main(String args[])
{
int n = 10 ;
for ( int i = 0 ; i < n; i++)
System.out.print(tribonacci(i) + " " );
System.out.println();
}
}
|
Python 3
def multiply(T, M):
a = (T[ 0 ][ 0 ] * M[ 0 ][ 0 ] + T[ 0 ][ 1 ] *
M[ 1 ][ 0 ] + T[ 0 ][ 2 ] * M[ 2 ][ 0 ])
b = (T[ 0 ][ 0 ] * M[ 0 ][ 1 ] + T[ 0 ][ 1 ] *
M[ 1 ][ 1 ] + T[ 0 ][ 2 ] * M[ 2 ][ 1 ])
c = (T[ 0 ][ 0 ] * M[ 0 ][ 2 ] + T[ 0 ][ 1 ] *
M[ 1 ][ 2 ] + T[ 0 ][ 2 ] * M[ 2 ][ 2 ])
d = (T[ 1 ][ 0 ] * M[ 0 ][ 0 ] + T[ 1 ][ 1 ] *
M[ 1 ][ 0 ] + T[ 1 ][ 2 ] * M[ 2 ][ 0 ])
e = (T[ 1 ][ 0 ] * M[ 0 ][ 1 ] + T[ 1 ][ 1 ] *
M[ 1 ][ 1 ] + T[ 1 ][ 2 ] * M[ 2 ][ 1 ])
f = (T[ 1 ][ 0 ] * M[ 0 ][ 2 ] + T[ 1 ][ 1 ] *
M[ 1 ][ 2 ] + T[ 1 ][ 2 ] * M[ 2 ][ 2 ])
g = (T[ 2 ][ 0 ] * M[ 0 ][ 0 ] + T[ 2 ][ 1 ] *
M[ 1 ][ 0 ] + T[ 2 ][ 2 ] * M[ 2 ][ 0 ])
h = (T[ 2 ][ 0 ] * M[ 0 ][ 1 ] + T[ 2 ][ 1 ] *
M[ 1 ][ 1 ] + T[ 2 ][ 2 ] * M[ 2 ][ 1 ])
i = (T[ 2 ][ 0 ] * M[ 0 ][ 2 ] + T[ 2 ][ 1 ] *
M[ 1 ][ 2 ] + T[ 2 ][ 2 ] * M[ 2 ][ 2 ])
T[ 0 ][ 0 ] = a
T[ 0 ][ 1 ] = b
T[ 0 ][ 2 ] = c
T[ 1 ][ 0 ] = d
T[ 1 ][ 1 ] = e
T[ 1 ][ 2 ] = f
T[ 2 ][ 0 ] = g
T[ 2 ][ 1 ] = h
T[ 2 ][ 2 ] = i
def power(T, n):
if (n = = 0 or n = = 1 ):
return ;
M = [[ 1 , 1 , 1 ],
[ 1 , 0 , 0 ],
[ 0 , 1 , 0 ]]
power(T, n / / 2 )
multiply(T, T)
if (n % 2 ):
multiply(T, M)
def tribonacci(n):
T = [[ 1 , 1 , 1 ],
[ 1 , 0 , 0 ],
[ 0 , 1 , 0 ]]
if (n = = 0 or n = = 1 ):
return 0
else :
power(T, n - 2 )
return T[ 0 ][ 0 ]
if __name__ = = "__main__" :
n = 10
for i in range (n):
print (tribonacci(i),end = " " )
print ()
|
C#
using System;
class GFG
{
static void multiply( int [,]T,
int [,]M)
{
int a, b, c, d, e, f, g, h, i;
a = T[0,0] * M[0,0] +
T[0,1] * M[1,0] +
T[0,2] * M[2,0];
b = T[0,0] * M[0,1] +
T[0,1] * M[1,1] +
T[0,2] * M[2,1];
c = T[0,0] * M[0,2] +
T[0,1] * M[1,2] +
T[0,2] * M[2,2];
d = T[1,0] * M[0,0] +
T[1,1] * M[1,0] +
T[1,2] * M[2,0];
e = T[1,0] * M[0,1] +
T[1,1] * M[1,1] +
T[1,2] * M[2,1];
f = T[1,0] * M[0,2] +
T[1,1] * M[1,2] +
T[1,2] * M[2,2];
g = T[2,0] * M[0,0] +
T[2,1] * M[1,0] +
T[2,2] * M[2,0];
h = T[2,0] * M[0,1] +
T[2,1] * M[1,1] +
T[2,2] * M[2,1];
i = T[2,0] * M[0,2] +
T[2,1] * M[1,2] +
T[2,2] * M[2,2];
T[0,0] = a;
T[0,1] = b;
T[0,2] = c;
T[1,0] = d;
T[1,1] = e;
T[1,2] = f;
T[2,0] = g;
T[2,1] = h;
T[2,2] = i;
}
static void power( int [,]T, int n)
{
if (n == 0 || n == 1)
return ;
int [,]M = {{ 1, 1, 1 },
{ 1, 0, 0 },
{ 0, 1, 0 }};
power(T, n / 2);
multiply(T, T);
if (n % 2 != 0)
multiply(T, M);
}
static int tribonacci( int n)
{
int [,]T = {{ 1, 1, 1 },
{ 1, 0, 0 },
{ 0, 1, 0 }};
if (n == 0 || n == 1)
return 0;
else
power(T, n - 2);
return T[0,0];
}
public static void Main()
{
int n = 10;
for ( int i = 0; i < n; i++)
Console.Write(tribonacci(i) + " " );
Console.WriteLine();
}
}
|
PHP
<?php
function multiply(& $T , $M )
{
$a = $T [0][0] * $M [0][0] +
$T [0][1] * $M [1][0] +
$T [0][2] * $M [2][0];
$b = $T [0][0] * $M [0][1] +
$T [0][1] * $M [1][1] +
$T [0][2] * $M [2][1];
$c = $T [0][0] * $M [0][2] +
$T [0][1] * $M [1][2] +
$T [0][2] * $M [2][2];
$d = $T [1][0] * $M [0][0] +
$T [1][1] * $M [1][0] +
$T [1][2] * $M [2][0];
$e = $T [1][0] * $M [0][1] +
$T [1][1] * $M [1][1] +
$T [1][2] * $M [2][1];
$f = $T [1][0] * $M [0][2] +
$T [1][1] * $M [1][2] +
$T [1][2] * $M [2][2];
$g = $T [2][0] * $M [0][0] +
$T [2][1] * $M [1][0] +
$T [2][2] * $M [2][0];
$h = $T [2][0] * $M [0][1] +
$T [2][1] * $M [1][1] +
$T [2][2] * $M [2][1];
$i = $T [2][0] * $M [0][2] +
$T [2][1] * $M [1][2] +
$T [2][2] * $M [2][2];
$T [0][0] = $a ;
$T [0][1] = $b ;
$T [0][2] = $c ;
$T [1][0] = $d ;
$T [1][1] = $e ;
$T [1][2] = $f ;
$T [2][0] = $g ;
$T [2][1] = $h ;
$T [2][2] = $i ;
}
function power(& $T , $n )
{
if ( $n == 0 || $n == 1)
return ;
$M = array ( array ( 1, 1, 1 ),
array ( 1, 0, 0 ),
array ( 0, 1, 0 ));
power( $T , (int)( $n / 2));
multiply( $T , $T );
if ( $n % 2)
multiply( $T , $M );
}
function tribonacci( $n )
{
$T = array ( array ( 1, 1, 1 ),
array ( 1, 0, 0 ),
array ( 0, 1, 0 ));
if ( $n == 0 || $n == 1)
return 0;
else
power( $T , $n - 2);
return $T [0][0];
}
$n = 10;
for ( $i = 0; $i < $n ; $i ++)
echo tribonacci( $i ) . " " ;
echo "\n" ;
?>
|
Javascript
<script>
function multiply(T , M)
{
var a, b, c, d, e, f, g, h, i;
a = T[0][0] * M[0][0] +
T[0][1] * M[1][0] +
T[0][2] * M[2][0];
b = T[0][0] * M[0][1] +
T[0][1] * M[1][1] +
T[0][2] * M[2][1];
c = T[0][0] * M[0][2] +
T[0][1] * M[1][2] +
T[0][2] * M[2][2];
d = T[1][0] * M[0][0] +
T[1][1] * M[1][0] +
T[1][2] * M[2][0];
e = T[1][0] * M[0][1] +
T[1][1] * M[1][1] +
T[1][2] * M[2][1];
f = T[1][0] * M[0][2] +
T[1][1] * M[1][2] +
T[1][2] * M[2][2];
g = T[2][0] * M[0][0] +
T[2][1] * M[1][0] +
T[2][2] * M[2][0];
h = T[2][0] * M[0][1] +
T[2][1] * M[1][1] +
T[2][2] * M[2][1];
i = T[2][0] * M[0][2] +
T[2][1] * M[1][2] +
T[2][2] * M[2][2];
T[0][0] = a;
T[0][1] = b;
T[0][2] = c;
T[1][0] = d;
T[1][1] = e;
T[1][2] = f;
T[2][0] = g;
T[2][1] = h;
T[2][2] = i;
}
function power(T , n)
{
if (n == 0 || n == 1)
return ;
var M = [[ 1, 1, 1 ],
[ 1, 0, 0 ],
[ 0, 1, 0 ]];
power(T, parseInt(n / 2));
multiply(T, T);
if (n % 2 != 0)
multiply(T, M);
}
function tribonacci(n)
{
var T = [[ 1, 1, 1 ],
[ 1, 0, 0 ],
[ 0, 1, 0 ]];
if (n == 0 || n == 1)
return 0;
else
power(T, n - 2);
return T[0][0];
}
var n = 10;
for ( var i = 0; i < n; i++)
document.write(tribonacci(i) + " " );
document.write( '<br>' );
</script>
|
Output
0 0 1 1 2 4 7 13 24 44
If you like GeeksforGeeks and would like to contribute, you can also write an article using write.geeksforgeeks.org or mail your article to review-team@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
Feeling lost in the world of random DSA topics, wasting time without progress? It's time for a change! Join our DSA course, where we'll guide you on an exciting journey to master DSA efficiently and on schedule.
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 geeks!
Last Updated :
29 Jul, 2022
Like Article
Save Article