forked from TheAlgorithms/JavaScript
- Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTribonacciNumber.js
More file actions
Latest commit
20 lines (19 loc) · 542 Bytes
/
Copy pathTribonacciNumber.js
File metadata and controls
20 lines (19 loc) · 542 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
/**
* @function Tribonacci
* @description Tribonacci is the sum of previous three tribonacci numbers.
* @param {Integer} n - The input integer
* @return {Integer} tribonacci of n.
* @see [Tribonacci_Numbers](https://www.geeksforgeeks.org/tribonacci-numbers/)
*/
consttribonacci=(n)=>{
// creating array to store previous tribonacci numbers
constdp=newArray(n+1)
dp[0]=0
dp[1]=1
dp[2]=1
for(leti=3;i<=n;i++){
dp[i]=dp[i-1]+dp[i-2]+dp[i-3]
}
returndp[n]
}
export{tribonacci}