📜  Tribonacci数字

📅  最后修改于: 2021-04-24 19:28:30             🧑  作者: Mango

tribonacci级数是Fibonacci序列的概括,其中每个项是前面三个项的总和。
Tribonacci序列:
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、989500096、181997601、334745777、615693474、1132436852等
Tribonacci号码的一般形式:

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

给定一个值N,任务是打印第一个N Tribonacci数字。
例子 :

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

一个简单的解决方案是简单地遵循递归公式并为其编写递归代码,

C++
// A simple recursive CPP program to print
// first n Tribinacci numbers.
#include 
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 Tribinacci 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 Tribinacci 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 Tribinacci 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.


PHP


Javascript


C++
// A DP based CPP
// program to print
// first n Tribinacci
// numbers.
#include 
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
// Tribinacci 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.*/


Python3
# A DP based
# Python 3
# program to print
# first n Tribinacci
# 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
// Tribinacci 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.*/


PHP


C++
// A space optimized
// based CPP program to
// print first n
// Tribinacci numbers.
#include 
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
// Tribinacci 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.


Python3
# A space optimized
# based Python 3
# program to print
# first n Tribinacci
# 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
// Tribinacci 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.


PHP
 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
?>


C++
#include 
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 3
# 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.


PHP


输出 :

0 0 1 1 2 4 7 13 24 44 

上述解决方案的时间复杂度是指数的。
更好的解决方案是使用动态编程。

C++

// A DP based CPP
// program to print
// first n Tribinacci
// numbers.
#include 
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
// Tribinacci 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.*/

Python3

# A DP based
# Python 3
# program to print
# first n Tribinacci
# 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
// Tribinacci 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.*/

的PHP


输出 :

0 0 1 1 2 4 7 13 24 44 

上面的时间复杂度是线性的,但是需要额外的空间。我们可以使用三个变量来优化前面解决方案中使用的空间,以跟踪前三个数字。

C++

// A space optimized
// based CPP program to
// print first n
// Tribinacci numbers.
#include 
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
// Tribinacci 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.

Python3

# A space optimized
# based Python 3
# program to print
# first n Tribinacci
# 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
// Tribinacci 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.

的PHP

 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
?>

输出 :

0 0 0 1 2 4 7 13 24 44  

下面是使用矩阵求幂的更有效的解决方案。

C++

#include 
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 3

# 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.

的PHP


输出 :

0 0 1 1 2 4 7 13 24 44