Tribonacci Numbers

2.6

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,

// A simple recursive CPP program to print
// first n Tribinacci numbers.
#include <iostream>
using namespace std;

int printTribRec(int n)
{
    if (n == 0 || n == 1)
        return 0;
    else if (n == 2)
        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;
}

Output:

0 0 1 1 2 4 7 13 24 44 

Time complexity of above solution is exponential.

A better solution is to use Dynamic Programming.

// A DP based CPP program to print
// first n Tribinacci 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;
}

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.

// A space optimized based CPP program to
// print first n Tribinacci numbers.
#include <iostream>
using namespace std;

void printTrib(int n)
{
    if (n < 1)
        return;

    int first = 0, second = 0, 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 << " ";
    }
}

// Driver code
int main()
{
    int n = 10;
    printTrib(n);
    return 0;
}

Output:

0 0 1 1 2 4 7 13 24 44 

This article is contributed by Sahil Rajput. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@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.

GATE CS Corner    Company Wise Coding Practice

Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.

Recommended Posts:



2.6 Average Difficulty : 2.6/5.0
Based on 5 vote(s)










Writing code in comment? Please use ide.geeksforgeeks.org, generate link and share the link here.