Logic Building
# Take n
n = int(input("Enter number of terms: "))
# Generate Tribonacci
if n <= 0:
print("Invalid input")
elif n == 1:
print("0")
elif n == 2:
print("0 1")
elif n == 3:
print("0 1 1")
else:
a, b, c = 0, 1, 1
print(a, b, c, end=" ")
for i in range(3, n):
d = a + b + c
print(d, end=" ")
a, b, c = b, c, d
print()Output
Enter number of terms: 8 0 1 1 2 4 7 13 24
Each term is sum of previous three.
Key Concepts:
- Start with 0, 1, 1
- Each next = sum of previous three
- Track last three terms