-
Notifications
You must be signed in to change notification settings - Fork 1k
/
Copy pathtribonacci.cpp
49 lines (37 loc) · 1.05 KB
/
tribonacci.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
/*
CPP program to find the n'th number in the tribonacci series
Tribonacci series is a generalization of the Fibonacci sequence, in which the current term
is the sum of the previous three terms
*/
#include<bits/stdc++.h>
using namespace std;
long int find_tribonacci(int n)
{
long int dp[n];
memset(dp, 0, sizeof(dp));
dp[2] = 1;
for(int i = 3;i < n; i++)
{
dp[i] = dp[i - 1] + dp[i - 2] + dp[i - 3];
}
return dp[n-1];
}
int main()
{
int n, ans;
cout<<"Enter the value of n?, where you need the n'th number in the tribonacci sequence. ";
cin >> n;
ans = find_tribonacci(n);
cout<<"The "<<n<<"'th term in the tribonacci series is "<<ans;
}
/*
Time Complexity - O(n)
Space Complexity - O(n)
SAMPLE INPUT AND OUTPUT
SAMPLE I
Enter the value of n?, where you need the n'th number in the tribonacci sequence. 12
The 12'th term in the tribonacci series is 149.
SAMPLE II
Enter the value of n?, where you need the n'th number in the tribonacci sequence. 23
The 23'th term in the tribonacci series is 121415
*/