Tribonacci Series

Generate Tribonacci series (each term is sum of previous three).

IntermediatePhase 6 - Category 4: Mathematical CombinationsExample 8 of 10
tribonacci-series.py
1# Take n
2n = int(input("Enter number of terms: "))
3
4# Generate Tribonacci
5if n <= 0:
6 print("Invalid input")
7elif n == 1:
8 print("0")
9elif n == 2:
10 print("0 1")
11elif n == 3:
12 print("0 1 1")
13else:
14 a, b, c = 0, 1, 1
15 print(a, b, c, end=" ")
16 for i in range(3, n):
17 d = a + b + c
18 print(d, end=" ")
19 a, b, c = b, c, d
20 print()

Output

Enter number of terms: 8
0 1 1 2 4 7 13 24

What's going on

Each term is sum of previous three.

Key Concepts:

Start with 0, 1, 1
Each next = sum of previous three
Track last three terms