Advertisement
GalinaKG

04. Tribonacci Sequence

Jun 15th, 2022
176
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.50 KB | None | 0 0
  1. def tribonacci_seq(num: int):
  2.     n1 = 1
  3.     n2 = 1
  4.     n3 = 2
  5.     if num == 1:
  6.         return '1'
  7.     elif num == 2:
  8.         return '1 1'
  9.     elif num == 3:
  10.         return '1 1 2'
  11.     else:
  12.         for i in range(4, num + 1):
  13.             current_num = n1 + n2 + n3
  14.             list_nums.append(str(current_num))
  15.             n1 = n2
  16.             n2 = n3
  17.             n3 = current_num
  18.  
  19.         return ' '.join(list_nums)
  20.  
  21.  
  22. list_nums = ['1', '1', '2']
  23.  
  24. print(tribonacci_seq(num=int(input())))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement