Table of contents
1.
Introduction
2.
Tribonacci Series
3.
Problem Statement
3.1.
Examples:
4.
Solution
4.1.
Method 1
4.2.
Method 2
4.3.
Method 3
4.4.
Method 4
5.
Frequently Asked Questions
5.1.
What is Fibonacci Series?
5.2.
What is Tribonacci Series?
5.3.
What are the values of the first three Tribonacci Numbers?
5.4.
What is a list in Python?
5.5.
What is a String in Python?
6.
Conclusion
Last Updated: Mar 27, 2024
Easy

Tribonacci Series In Python

Author Rajat Agrawal
0 upvote

Introduction

Tribonacci series consists of numbers defined by the recurrence F(N) = F(N-1)+F(N-2)+F(N-3), which is similar to the Fibonacci sequence in which the previous two terms determine the following number; however, the Tribonacci series requires the preceding three terms.

The first three terms of the Tribonacci Series are 0, 0, and 1.

Some of terms from the Tribonacci Series is given below:-

0, 0, 1, 1, 2, 4, 7, 13, 24, 44, 81, 149, 274, 504, 927, 1705, 3136, 5768, 10609, 19513, 35890, 66012, 121415, 223317, etc.

Let’s learn how to Implement the Tribonacci Series in Python using different approaches.

Also see, Merge Sort Python

Tribonacci Series

A Tribonacci series is written as follows:

The first three terms of the Tribonacci Series are 0, 0, and 1.

The recurrence relation that defines the Tribonacci Series is given below.

General Form of Tribonacci Series:

F(N) = F(N-1)+F(N-2)+F(N-3), where N denotes the Nth Tribonacci Number.
with
F(0) = F(1) = 0 and F(2) = 1.
You can also try this code with Online Python Compiler
Run Code

 

Check out Escape Sequence in Python

Problem Statement

Given a value N, your task is to print the Nth Tribonacci Number.

Examples:

Input: N = 4

Output:

Input: N = 8

Output: 24

Explanation: The Nth Tribonacci number can be calculated by using the recursive formula, for N = 4 it will give 2, and for N = 8  it will give 24 as output.

Solution Problem Icon

There can be multiple approaches to solve this problem. At first, we will see the Brute Force solution, and then move on to the optimized solution. 

Method 1

The most naive way to solve this problem is to Solve using Recursion with the help of the given recurrence relation.

Let’s see the Code Implementation for Tribonacci Series in python. Desktop Icon

# Tribonacci Series in python
# nth tribonacci number

def nthTribonacci(n):
    if(n==0 or n==1):
        return 0
    if(n==2):
        return 1
    
    return nthTribonacci(n-1)+nthTribonacci(n-2)+nthTribonacci(n-3)

n = 15
nthTribonacciNumber = nthTribonacci(n)
print(f'The {n}th Tribonacci Number is:',nthTribonacciNumber)
You can also try this code with Online Python Compiler
Run Code


Output

The 15th Tribonacci Number is: 1705
You can also try this code with Online Python Compiler
Run Code


Time Complexity: Exponential, approximately equal to O(N^3). As we are making three recursive calls.

Space Complexity: O(N), as auxiliary space complexity. A maximum of elements can be present in the Recursive Stack Space at a time.

Method 2

A better solution to the problem would be to use Dynamic Programming.

TopDown DP - Memoization

We can memoize the recursive solution provided in Method 1. This will optimize the solution very much. 

Since there is a single changing variable in the Recursion, therefore we will create 1-D DP.

dp[n]. Initialize the DP with -1.

Let’s see the Code Implementation for Tribonacci Series in python. Desktop Icon

# Tribonacci Series in python
# nth tribonacci number

def nthTribonacci(n):
    dp = [-1]*21
    dp[0] = dp[1] = 0
    dp[2] = 1
    def tribo(n):
        if(n==0 or n==1):
            return 0
        if(n==2):
            return 1
        if(dp[n]!=-1):
            return dp[n]
        nthNumber = nthTribonacci(n-1)+nthTribonacci(n-2)+nthTribonacci(n-3)
        dp[n] = nthNumber
        return nthNumber
    return tribo(n)

n = 20
nthTribonacciNumber = nthTribonacci(n)
print(f'The {n}th Tribonacci Number is:',nthTribonacciNumber)
You can also try this code with Online Python Compiler
Run Code


Output

The 20th Tribonacci Number is: 35890
You can also try this code with Online Python Compiler
Run Code

You can practice by yourself with the help of online python compiler for better understanding.
Time Complexity: O(N). Since we are storing the previous results into a list at max N recursive calls will happen.

Space Complexity: O(N), as we have declared a list of size N.

Also, O(N) is auxiliary space complexity. A maximum of elements can be present in the Recursive Stack Space at a time.

Method 3

This method will help in reducing the Auxilary Space complexity used in Method 2.

Bottom Up DP - Tabulation

We will initialize our dp list with 0. dp[n]

Let’s see the Code Implementation for Tribonacci Series in python.Desktop Icon

# Tribonacci Series in python
# nth tribonacci number

def nthTribonacci(n):
    dp = [0 for i in range(n + 1)]
    dp[0] = dp[1] = 0
    dp[2] = 1

    for i in range(3,n+1):
        dp[i] = dp[i - 1] + dp[i - 2] + dp[i - 3]
    return dp[n]

n = 18
nthTribonacciNumber = nthTribonacci(n)
print(f'The {n}th Tribonacci Number is:',nthTribonacciNumber)
You can also try this code with Online Python Compiler
Run Code


Output

The 18th Tribonacci Number is: 10609
You can also try this code with Online Python Compiler
Run Code


Time Complexity: O(N). Since we are running a for loop up to N times.

Space Complexity: O(N), as we have declared a list of size N.

Method 4

This method will solve this problem in O(1) Space Complexity.

Instead of taking a list of size N, we will take three variables to store the previous three results as our current result is completely dependent only on the previous three elements.

Space Optimization

Taking three variables, prev1=1, prev2=0, prev3=0.

Let’s see the Code Implementation for Tribonacci Series in python. Desktop Icon

# Tribonacci Series in python
# nth tribonacci number

def nthTribonacci(n):
    prev3=0
    prev2=0
    prev1 = 1
    for i in range(3,n+1):
        curr = prev1+prev2+prev3
        prev3 = prev2
        prev2 = prev1
        prev1 = curr
    return prev1

n = 18
nthTribonacciNumber = nthTribonacci(n)
print(f'The {n}th Tribonacci Number is:',nthTribonacciNumber)
You can also try this code with Online Python Compiler
Run Code


Output

The 18th Tribonacci Number is: 10609
You can also try this code with Online Python Compiler
Run Code


Time Complexity: O(N), Since we are running a for loop up to N times.

Space Complexity: O(1), here we are taking any extra space instead we just have used three variables to store previous values.

Also check out - Rod Cutting Problem

You can also read about Palindrome Number in Python here.

Frequently Asked Questions

What is Fibonacci Series?

Fibonacci Series is a series of numbers in which each number (Fibonacci number) is the sum of the two preceding numbers. It is defined by the recurrence F(N) = F(N-1)+F(N-2).

What is Tribonacci Series?

Tribonacci series consists of numbers defined by the recurrence F(N) = F(N-1)+F(N-2)+F(N-3), which is similar to the Fibonacci sequence in which the previous two terms determine the following number; however, the Tribonacci series requires the preceding three terms.

What are the values of the first three Tribonacci Numbers?

The first three Tribonacci Number values are 0, 0, and 1, respectively.

What is a list in Python?

 Lists in Python is a data structure used to store multiple items in a single variable.

What is a String in Python?

A string in Python is a sequence of characters.

Conclusion

In this article, we have extensively discussed Tribonacci Series in Python and its recurrence relation, and also we have implemented it in Python using different Methods.

If you want to learn more, check out our articles on Flatten a Multi-level Linked List (Depth wise), Construct the Full K-ary Tree from its Preorder TraversalSum of all Unique Sub-array Sum for a Given Array, and Clone a Binary Tree with Random Pointers.

Do upvote our blog to help other ninjas grow.

Happy Coding!

Live masterclass