mirror of
https://github.com/TheAlgorithms/Python.git
synced 2024-12-05 02:40:16 +00:00
02d89bde67
* Added implementation for Tribonacci sequence using dp * Updated parameter name * Apply suggestions from code review --------- Co-authored-by: Tianyi Zheng <tianyizheng02@gmail.com>
25 lines
452 B
Python
25 lines
452 B
Python
# Tribonacci sequence using Dynamic Programming
|
|
|
|
|
|
def tribonacci(num: int) -> list[int]:
|
|
"""
|
|
Given a number, return first n Tribonacci Numbers.
|
|
>>> tribonacci(5)
|
|
[0, 0, 1, 1, 2]
|
|
>>> tribonacci(8)
|
|
[0, 0, 1, 1, 2, 4, 7, 13]
|
|
"""
|
|
dp = [0] * num
|
|
dp[2] = 1
|
|
|
|
for i in range(3, num):
|
|
dp[i] = dp[i - 1] + dp[i - 2] + dp[i - 3]
|
|
|
|
return dp
|
|
|
|
|
|
if __name__ == "__main__":
|
|
import doctest
|
|
|
|
doctest.testmod()
|