0
0
PythonProgramBeginner · 2 min read

Python Program to Print Tribonacci Series

You can print the tribonacci series in Python by starting with 0, 1, 1 and then printing each next number as the sum of the previous three using a loop, for example: trib = [0, 1, 1]; for i in range(3, n): trib.append(trib[i-1] + trib[i-2] + trib[i-3]).
📋

Examples

Input5
Output0 1 1 2 4
Input10
Output0 1 1 2 4 7 13 24 44 81
Input1
Output0
🧠

How to Think About It

To print the tribonacci series, start with the first three fixed numbers 0, 1, and 1. Then, for each next number, add the previous three numbers together. Repeat this until you have printed the desired count of numbers.
📐

Algorithm

1
Start with a list containing the first three tribonacci numbers: 0, 1, 1.
2
If the requested count is less than or equal to 3, print only that many numbers from the start.
3
Otherwise, use a loop from 3 to the requested count minus one.
4
In each loop iteration, calculate the next number by adding the last three numbers in the list.
5
Append this new number to the list.
6
After the loop, print all numbers in the list up to the requested count.
💻

Code

python
def tribonacci(n):
    trib = [0, 1, 1]
    if n <= 3:
        return trib[:n]
    for i in range(3, n):
        trib.append(trib[i-1] + trib[i-2] + trib[i-3])
    return trib

n = int(input("Enter the number of terms: "))
result = tribonacci(n)
print(' '.join(map(str, result)))
Output
Enter the number of terms: 10 0 1 1 2 4 7 13 24 44 81
🔍

Dry Run

Let's trace the tribonacci series for n=5 through the code

1

Initialize list

trib = [0, 1, 1]

2

Check if n <= 3

n=5, so continue to loop

3

Loop iteration i=3

trib[2] + trib[1] + trib[0] = 1 + 1 + 0 = 2; trib = [0, 1, 1, 2]

4

Loop iteration i=4

trib[3] + trib[2] + trib[1] = 2 + 1 + 1 = 4; trib = [0, 1, 1, 2, 4]

5

End loop and return

Return [0, 1, 1, 2, 4]

itribonacci list
Start[0, 1, 1]
3[0, 1, 1, 2]
4[0, 1, 1, 2, 4]
💡

Why This Works

Step 1: Start with base numbers

The tribonacci series begins with fixed numbers 0, 1, 1 which are the first three terms.

Step 2: Calculate next terms

Each new term is the sum of the previous three terms, so we add trib[i-1] + trib[i-2] + trib[i-3].

Step 3: Use a loop to generate terms

A loop runs from the 4th term to the nth term, appending each new calculated term to the list.

Step 4: Print the series

Finally, the program prints all terms up to the requested count.

🔄

Alternative Approaches

Recursive approach
python
def tribonacci_recursive(n):
    if n == 0:
        return []
    elif n == 1:
        return [0]
    elif n == 2:
        return [0, 1]
    elif n == 3:
        return [0, 1, 1]
    else:
        seq = tribonacci_recursive(n-1)
        seq.append(seq[-1] + seq[-2] + seq[-3])
        return seq

n = int(input("Enter the number of terms: "))
result = tribonacci_recursive(n)
print(' '.join(map(str, result)))
This method is simple but inefficient for large n due to repeated calculations and recursion overhead.
Using three variables without list
python
def tribonacci_vars(n):
    a, b, c = 0, 1, 1
    result = []
    for _ in range(n):
        result.append(a)
        a, b, c = b, c, a + b + c
    return result

n = int(input("Enter the number of terms: "))
print(' '.join(map(str, tribonacci_vars(n))))
This approach uses less memory by not storing the entire list during calculation but still returns the full series.

Complexity: O(n) time, O(n) space

Time Complexity

The program runs a single loop from 3 to n, so it takes linear time O(n).

Space Complexity

It stores all n terms in a list, so space complexity is O(n).

Which Approach is Fastest?

The iterative list approach is fastest and simplest; recursion is slower and uses more call stack memory.

ApproachTimeSpaceBest For
Iterative listO(n)O(n)General use, easy to understand
RecursiveO(n)O(n) call stackSmall n, educational purposes
Three variablesO(n)O(n)Memory efficient during calculation
💡
Start with the first three numbers fixed and build the series by adding the last three numbers repeatedly.
⚠️
Beginners often forget to handle the first three terms separately or try to sum fewer than three previous terms.