fork download
  1. def iterative_trib(n):
  2. if n <= 1:
  3. return 1
  4. elif n == 2:
  5. return 2
  6. else:
  7. a, b, c = 1, 1, 2
  8. while n > 2:
  9. a, b, c = b, c, a+b+c
  10. n = n - 1
  11. return c
  12.  
  13. def trib(n):
  14. if n <= 1:
  15. return 1
  16. elif n == 2:
  17. return 2
  18. else:
  19. return trib(n-1) + trib(n-2) + trib(n-3)
  20.  
  21. print("Recursive")
  22. for i in range(23, 30):
  23. print(f"t({i})={trib(i)}" + (", " if i != 29 else "\n"), end="")
  24.  
  25. print("Iterative")
  26. for i in range(30, 37):
  27. print(f"t({i})={iterative_trib(i)}" + (", " if i != 36 else "\n"), end="")
Success #stdin #stdout 9.14s 9608KB
stdin
Standard input is empty
stdout
Recursive
t(23)=755476, t(24)=1389537, t(25)=2555757, t(26)=4700770, t(27)=8646064, t(28)=15902591, t(29)=29249425
Iterative
t(30)=53798080, t(31)=98950096, t(32)=181997601, t(33)=334745777, t(34)=615693474, t(35)=1132436852, t(36)=2082876103