Skip to content
geeksforgeeks
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • Tutorials
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
  • Practice
    • Build your AI Agent
    • GfG 160
    • Problem of the Day
    • Practice Coding Problems
    • GfG SDE Sheet
  • Contests
    • Accenture Hackathon (Ending Soon!)
    • GfG Weekly [Rated Contest]
    • Job-A-Thon Hiring Challenge
    • All Contests and Events
  • DSA
  • Practice Mathematical Algorithm
  • Mathematical Algorithms
  • Pythagorean Triplet
  • Fibonacci Number
  • Euclidean Algorithm
  • LCM of Array
  • GCD of Array
  • Binomial Coefficient
  • Catalan Numbers
  • Sieve of Eratosthenes
  • Euler Totient Function
  • Modular Exponentiation
  • Modular Multiplicative Inverse
  • Stein's Algorithm
  • Juggler Sequence
  • Chinese Remainder Theorem
  • Quiz on Fibonacci Numbers
Open In App
Next Article:
Geek-onacci Number
Next article icon

Tribonacci Numbers

Last Updated : 13 Nov, 2024
Comments
Improve
Suggest changes
Like Article
Like
Report
Try it on GfG Practice
redirect icon

The tribonacci series is a generalization of the Fibonacci sequence where each term is the sum of the three preceding terms.

a(n) = a(n-1) + a(n-2) + a(n-3)
with
a(0) = a(1) = 0, a(2) = 1.

First few numbers in the Tribonacci Sequence are 0, 0, 1, 1, 2, 4, 7, 13, 24, 44, 81, 149, 274, 504, ….

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++
// A simple recursive CPP program to print // first n Tribonacci numbers. #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) << " "; }  // Driver code int main() {     int n = 10;     printTrib(n);     return 0; } 
Java
// A simple recursive CPP program // first n Tribonacci numbers. import java.io.*;  class GFG {          // Recursion Function     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)                              +" ");     }           // Driver code     public static void main(String args[])     {         int n = 10;          printTrib(n);     } }  // This code is contributed by Nikita tiwari. 
Python
# A simple recursive CPP program to print # first n Tribonacci numbers.  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 = "")           # Driver code n = 10 printTrib(n)   # This code is contributed by Nikita Tiwari. 
C#
// A simple recursive C# program // first n Tribinocci numbers. using System;  class GFG {          // Recursion Function     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)                                     +" ");     }          // Driver code     public static void Main()     {         int n = 10;          printTrib(n);     } }  // This code is contributed by vt_m. 
JavaScript
<script> // A simple recursive Javascript program to // print first n Tribinocci numbers.  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) + " "); }      // Driver Code     let n = 10;     printTrib(n);  // This code is contributed by _saurabh_jaiswal </script> 
PHP
<?php // A simple recursive PHP program to  // print first n Tribinocci numbers.  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), " "; }      // Driver Code     $n = 10;     printTrib($n);  // This code is contributed by ajit ?> 

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++
// A simple recursive CPP program to print // first n Tribonacci numbers. #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) {     // dp vector to store subproblems     vector<int> dp(n+1, -1) ;     for (int i = 1; i < n; i++)         cout << printTribRec(i, dp) << " "; }  // Driver code int main() {     int n = 10;     printTrib(n);     return 0; } 
Java
// A simple recursive Java program to print // first n Tribonacci numbers.  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)     {         // dp vector to store subproblems         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) + " ");     }      // Driver code     public static void main(String[] args)     {         int n = 10;         printTrib(n);     } }  // This code is contributed by phasing17 
Python
def tribonacci(n):      h={} #creating the dictionary to store the results     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 #storing the results so that we can reuse it again          return res     return tribo(n) n=10 for i in range(n):     print(tribonacci(i),end=' ') 
C#
// A simple recursive C# program to print // first n Tribonacci numbers.  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)     {         // dp vector to store subproblems         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) + " ");     }      // Driver code     public static void Main(string[] args)     {         int n = 10;         printTrib(n);     } }  // This code is contributed by phasing17 
JavaScript
// A simple recursive JS program to print // first n Tribonacci numbers.  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) {     // dp vector to store subproblems     let dp = new Array(n+1).fill(-1) ;     for (var i = 1; i < n; i++)         process.stdout.write(printTribRec(i, dp) + " "); }  // Driver code let n = 10; printTrib(n);   // This code is contributed by phasing17 

Output
0 0 1 1 2 4 7 13 24 

2) Bottom-Up DP Tabulation:
 

C++
// A DP based CPP // program to print // first n Tribonacci // numbers. #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] << " "; }  // Driver code int main() {     int n = 10;     printTrib(n);     return 0; } 
Java
// A DP based Java program // to print first n // Tribonacci numbers. 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] + " ");     }          // Driver code     public static void main(String args[])     {         int n = 10;         printTrib(n);     } }  /* This code is contributed by Nikita Tiwari.*/ 
Python
# A DP based # Python 3  # program to print # first n Tribonacci # numbers.  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="")           # Driver code n = 10 printTrib(n)  # This code is contributed by Nikita Tiwari. 
C#
// A DP based C# program // to print first n // Tribonacci numbers. 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] + " ");     }          // Driver code     public static void Main()     {         int n = 10;         printTrib(n);     } }  /* This code is contributed by vt_m.*/ 
JavaScript
<script> // Javascript program // to print first n // Tribonacci numbers.     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] + " ");     }    // driver function         let n = 10;         printTrib(n);      // This code is contributed by code_hunt. </script>     
PHP
<?php // A DP based PHP program  // to print first n  // Tribonacci numbers.  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] ," "; }  // Driver code $n = 10; printTrib($n);  // This code is contributed by ajit ?> 

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++
// A space optimized // based CPP program to // print first n // Tribonacci numbers. #include <iostream> using namespace std;  void printTrib(int n) {     if (n < 1)         return;      // Initialize first     // three numbers     int first = 0, second = 0;     int third = 1;      cout << first << " ";     if (n > 1)         cout << second << " ";          if (n > 2)         cout << second << " ";      // Loop to add previous      // three numbers for     // each number starting      // from 3 and then assign     // first, second, third     // to second, third, and      // curr to third respectively     for (int i = 3; i < n; i++)      {         int curr = first + second + third;         first = second;         second = third;         third = curr;          cout << curr << " ";     } }  // Driver code int main() {     int n = 10;     printTrib(n);     return 0; } 
Java
// A space optimized // based Java program // to print first n  // Tribinocci numbers. import java.io.*;  class GFG {          static void printTrib(int n)     {         if (n < 1)             return;              // Initialize first         // three numbers         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 + " ");              // Loop to add previous         // three numbers for         // each number starting         // from 3 and then assign         // first, second, third         // to second, third, and curr         // to third respectively         for (int i = 3; i < n; i++)          {             int curr = first + second + third;             first = second;             second = third;             third = curr;                  System.out.print(curr +" ");         }     }          // Driver code     public static void main(String args[])     {         int n = 10;         printTrib(n);     } }  // This code is contributed by Nikita Tiwari. 
Python
# A space optimized # based Python 3  # program to print # first n Tribinocci  # numbers.  def printTrib(n) :     if (n < 1) :         return       # Initialize first     # three numbers     first = 0     second = 0     third = 1      print( first , " ", end="")     if (n > 1) :         print(second, " ",end="")     if (n > 2) :         print(second, " ", end="")      # Loop to add previous     # three numbers for     # each number starting     # from 3 and then assign     # first, second, third     # to second, third, and curr     # to third respectively     for i in range(3, n) :         curr = first + second + third         first = second         second = third         third = curr          print(curr , " ", end="")           # Driver code n = 10 printTrib(n)  # This code is contributed by Nikita Tiwari. 
C#
// A space optimized // based C# program // to print first n  // Tribinocci numbers. using System;  class GFG {          static void printTrib(int n)     {         if (n < 1)             return;              // Initialize first         // three numbers         int first = 0, second = 0;         int third = 1;              Console.Write(first + " ");         if (n > 1)         Console.Write(second + " ");                  if (n > 2)         Console.Write(second + " ");              // Loop to add previous         // three numbers for         // each number starting         // from 3 and then assign         // first, second, third         // to second, third, and curr         // to third respectively         for (int i = 3; i < n; i++)          {             int curr = first + second + third;             first = second;             second = third;             third = curr;                  Console.Write(curr +" ");         }     }          // Driver code     public static void Main()     {         int n = 10;         printTrib(n);     } }  // This code is contributed by vt_m. 
JavaScript
<script>  // A space optimized // based Javascript program // to print first n // Tribonacci numbers.          function printTrib(n)     {         if (n < 1)             return;               // Initialize first         // three numbers         let first = 0, second = 0;         let third = 1;               document.write(first + " ");         if (n > 1)             document.write(second + " ");                   if (n > 2)             document.write(second + " ");               // Loop to add previous         // three numbers for         // each number starting         // from 3 and then assign         // first, second, third         // to second, third, and curr         // to third respectively         for (let i = 3; i < n; i++)         {             let curr = first + second + third;             first = second;             second = third;             third = curr;                   document.write(curr +" ");         }     }          // Driver code     let n = 10;     printTrib(n);          // This code is contributed by rag2127      </script> 
PHP
<?php // A space optimized // based PHP program to // print first n // Tribinocci numbers.|  function printTrib($n) {     if ($n < 1)         return;      // Initialize first     // three numbers     $first = 0; $second = 0;     $third = 1;      echo $first, " ";     if ($n > 1)         echo $second , " ";          if ($n > 2)         echo $second , " ";      // Loop to add previous      // three numbers for     // each number starting      // from 3 and then assign     // first, second, third     // to second, third, and      // curr to third respectively     for ($i = 3; $i < $n; $i++)      {         $curr = $first + $second + $third;         $first = $second;         $second = $third;         $third = $curr;          echo $curr , " ";     } }      // Driver code     $n = 10;     printTrib($n);  // This code is contributed by m_kit ?> 

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;  // Program to print first n  // tribonacci numbers Matrix  // Multiplication function  // for 3*3 matrix 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; }  // Recursive function to raise  // the matrix T to the power n void power(int T[3][3], int n) {     // base condition.     if (n == 0 || n == 1)         return;     int M[3][3] = {{ 1, 1, 1 },                     { 1, 0, 0 },                     { 0, 1, 0 }};      // recursively call to     // square the matrix     power(T, n / 2);      // calculating square      // of the matrix T     multiply(T, T);      // if n is odd multiply      // it one time with M     if (n % 2)         multiply(T, M); } int tribonacci(int n) {     int T[3][3] = {{ 1, 1, 1 },                     { 1, 0, 0 },                    { 0, 1, 0 }};      // base condition     if (n == 0 || n == 1)         return 0;     else         power(T, n - 2);      // T[0][0] contains the      // tribonacci number so     // return it     return T[0][0]; }  // Driver Code int main() {     int n = 10;     for (int i = 0; i < n; i++)         cout << tribonacci(i) << " ";     cout << endl;     return 0; } 
Java
// Java Program to print // first n tribonacci numbers // Matrix Multiplication // function for 3*3 matrix 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;     }          // Recursive function to raise      // the matrix T to the power n     static void power(int T[][], int n)     {         // base condition.         if (n == 0 || n == 1)             return;         int M[][] = {{ 1, 1, 1 },                       { 1, 0, 0 },                       { 0, 1, 0 }};              // recursively call to          // square the matrix         power(T, n / 2);              // calculating square          // of the matrix T         multiply(T, T);              // if n is odd multiply         // it one time with M         if (n % 2 != 0)             multiply(T, M);     }     static int tribonacci(int n)     {         int T[][] = {{ 1, 1, 1 },                       { 1, 0, 0 },                      { 0, 1, 0 }};              // base condition         if (n == 0 || n == 1)             return 0;         else             power(T, n - 2);              // T[0][0] contains the          // tribonacci number so         // return it         return T[0][0];     }          // Driver Code     public static void main(String args[])     {         int n = 10;         for (int i = 0; i < n; i++)         System.out.print(tribonacci(i) + " ");         System.out.println();     } }  // This code is contributed by Nikita Tiwari. 
Python
# Program to print first n tribonacci  # numbers Matrix Multiplication # function for 3*3 matrix 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  # Recursive function to raise  # the matrix T to the power n def power(T, n):      # base condition.     if (n == 0 or n == 1):         return;     M = [[ 1, 1, 1 ],                  [ 1, 0, 0 ],                  [ 0, 1, 0 ]]      # recursively call to     # square the matrix     power(T, n // 2)      # calculating square      # of the matrix T     multiply(T, T)      # if n is odd multiply      # it one time with M     if (n % 2):         multiply(T, M)  def tribonacci(n):          T = [[ 1, 1, 1 ],          [1, 0, 0 ],         [0, 1, 0 ]]      # base condition     if (n == 0 or n == 1):         return 0     else:         power(T, n - 2)      # T[0][0] contains the      # tribonacci number so     # return it     return T[0][0]  # Driver Code if __name__ == "__main__":     n = 10     for i in range(n):         print(tribonacci(i),end=" ")     print()  # This code is contributed by ChitraNayal 
C#
// C# Program to print // first n tribonacci numbers // Matrix Multiplication // function for 3*3 matrix 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;     }          // Recursive function to raise      // the matrix T to the power n     static void power(int [,]T, int n)     {         // base condition.         if (n == 0 || n == 1)             return;         int [,]M = {{ 1, 1, 1 },                      { 1, 0, 0 },                      { 0, 1, 0 }};              // recursively call to          // square the matrix         power(T, n / 2);              // calculating square          // of the matrix T         multiply(T, T);              // if n is odd multiply          // it one time with M         if (n % 2 != 0)             multiply(T, M);     }          static int tribonacci(int n)     {         int [,]T = {{ 1, 1, 1 },                      { 1, 0, 0 },                     { 0, 1, 0 }};              // base condition         if (n == 0 || n == 1)             return 0;         else             power(T, n - 2);              // T[0][0] contains the          // tribonacci number so         // return it         return T[0,0];     }          // Driver Code     public static void Main()     {         int n = 10;         for (int i = 0; i < n; i++)         Console.Write(tribonacci(i) + " ");         Console.WriteLine();     } }  // This code is contributed by vt_m. 
JavaScript
<script>  // javascript Program to print // first n tribonacci numbers // Matrix Multiplication // function for 3*3 matrix      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;     }          // Recursive function to raise      // the matrix T to the power n     function power(T , n)     {         // base condition.         if (n == 0 || n == 1)             return;         var M = [[ 1, 1, 1 ],                       [ 1, 0, 0 ],                       [ 0, 1, 0 ]];              // recursively call to          // square the matrix         power(T, parseInt(n / 2));              // calculating square          // of the matrix T         multiply(T, T);              // if n is odd multiply         // it one time with M         if (n % 2 != 0)             multiply(T, M);     }     function tribonacci(n)     {         var T = [[ 1, 1, 1 ],                       [ 1, 0, 0 ],                      [ 0, 1, 0 ]];              // base condition         if (n == 0 || n == 1)             return 0;         else             power(T, n - 2);              // T[0][0] contains the          // tribonacci number so         // return it         return T[0][0];     }          // Driver Code     var n = 10;     for (var i = 0; i < n; i++)     document.write(tribonacci(i) + " ");     document.write('<br>');   // This code contributed by shikhasingrajput  </script> 
PHP
 <?php // Program to print first n tribonacci numbers  // Matrix Multiplication function for 3*3 matrix 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; }  // Recursive function to raise  // the matrix T to the power n function power(&$T,$n) {     // base condition.     if ($n == 0 || $n == 1)         return;     $M = array(array( 1, 1, 1 ),                 array( 1, 0, 0 ),                 array( 0, 1, 0 ));      // recursively call to     // square the matrix     power($T, (int)($n / 2));      // calculating square      // of the matrix T     multiply($T, $T);      // if n is odd multiply      // it one time with M     if ($n % 2)         multiply($T, $M); }  function tribonacci($n) {     $T = array(array( 1, 1, 1 ),                 array( 1, 0, 0 ),                array( 0, 1, 0 ));      // base condition     if ($n == 0 || $n == 1)         return 0;     else         power($T, $n - 2);      // $T[0][0] contains the tribonacci      // number so return it     return $T[0][0]; }  // Driver Code $n = 10; for ($i = 0; $i < $n; $i++)     echo tribonacci($i) . " "; echo "\n";  // This code is contributed by mits ?> 

Output
0 0 1 1 2 4 7 13 24 44 


 



Next Article
Geek-onacci Number
https://media.geeksforgeeks.org/auth/avatar.png
GeeksforGeeks
Improve
Article Tags :
  • DSA
  • Mathematical
  • series
Practice Tags :
  • Mathematical
  • series

Similar Reads

  • Tetranacci Numbers
    The tetranacci numbers are a generalization of the Fibonacci numbers defined by the recurrence relation T(n) = T(n-1) + T(n-2) + T(n-3) + T(n-4) with T(0)=0, T(1)=1, T(2)=1, T(3)=2, For n>=4. They represent the n=4 case of the Fibonacci n-step numbers. The first few terms for n=0, 1, ... are 0, 1
    13 min read
  • Pentanacci Numbers
    The Pentanacci series is a generalization of the Fibonacci sequence where each term is the sum of the five preceding terms. The first few Pentanacci numbers are as follows - 0, 0, 0, 0, 1, 1, 2, 4, 8, 16, 31, 61, 120, 236, 464, 912, 1793, 3525, 6930, 13624, 26784, 52656, 103519..... Nth Term of Pent
    9 min read
  • Geek-onacci Number
    Given four integers A, B, C and N, where A, B, C represents the first three numbers of the geekonacci series, the task is to find the Nth term of the geekonacci series. The Nth term of the geekonacci series is the sum of its previous three terms in the series i.e., sum of (N - 1)th, (N - 2)th, and (
    13 min read
  • Triangular Numbers
    A number is termed a triangular number if we can represent it in the form of a triangular grid of points such that the points form an equilateral triangle and each row contains as many points as the row number, i.e., the first row has one point, second row has two points, third row has three points
    11 min read
  • Tridecagonal Number
    Given a number N, the task is to find the Nth Tridecagonal number. A tridecagonal number is a figurate number that extends the concept of triangular and square numbers to the tridecagon(a thirteen-sided polygon). The Nth tridecagonal number counts the number of dots in a pattern of N nested tridecag
    3 min read
  • Triacontagon Number
    Given a number N, the task is to find Nth Triacontagon number. An Triacontagon number is class of figurate number. It has 30 - sided polygon called triacontagon. The N-th triacontagonal number count’s the 30 number of dots and all others dots are surrounding with a common sharing corner and make a p
    3 min read
  • Triacontakaidigon Number
    Given a number N, the task is to find Nth Triacontakaidigon number. A Triacontakaidigon number is class of figurate number. It has 32 - sided polygon called triacontakaidigon. The N-th triacontakaidigon number count’s the 32 number of dots and all others dots are surrounding with a common sharing co
    3 min read
  • Taxicab Numbers
    The nth Taxicab number Taxicab(n), also called the n-th Hardy-Ramanujan number, is defined as the smallest number that can be expressed as a sum of two positive cube numbers in n distinct ways. The most famous taxicab number is 1729 = Taxicab(2) = (1 ^ 3) + (12 ^ 3) = (9 ^ 3) + (10 ^ 3).Given a numb
    6 min read
  • Tetradic Number
    A Tetradic Number (sometimes called a four-way number) is a number that remains the same when flipped back-front, mirrored up-down, or flipped up-down. In other words, a Tetradic Number is a palindromic number containing only 0, 1 and 8 as digits in the number. The first few Tetradic Number are: 0,
    11 min read
  • Octagonal number
    You are given a number n, the task is to find nth octagonal number. Also, find the Octagonal series till n.An octagonal number is the figure number that represent octagonal. Octagonal numbers can be formed by placing triangular numbers on the four sides of a square. Octagonal number is calculated by
    5 min read
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences