Advertisement
DiYane

Tribonacci sequence

Sep 20th, 2023
541
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.39 KB | None | 0 0
  1. def tribonacci(n):
  2.     if n <= 0:
  3.         return []
  4.  
  5.     sequence = [1, 1, 2]
  6.  
  7.     if n <= 3:
  8.         return sequence[:n]
  9.  
  10.     for i in range(3, n):
  11.         next_term = sequence[i - 1] + sequence[i - 2] + sequence[i - 3]
  12.         sequence.append(next_term)
  13.  
  14.     return sequence
  15.  
  16. n = int(input())
  17.  
  18. tribonacci_sequence = tribonacci(n)
  19. print(" ".join(map(str, tribonacci_sequence)))
  20.  
Tags: python
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement