Skip to content

Commit 0c4ee18

Browse files
tribonacci number
1 parent b7124b2 commit 0c4ee18

File tree

1 file changed

+19
-0
lines changed

1 file changed

+19
-0
lines changed

N-th-Tribonacci_Number.java

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
class Solution {
2+
public int tribonacci(int n) {
3+
4+
if(n <= 2){
5+
return (n == 0) ? 0 : 1;
6+
}
7+
8+
int T_0 = 0, T_1 = 1, T_2 = 1, sum = 0;
9+
10+
for(int i = 3; i <= n; i++){
11+
sum = T_0 + T_1 + T_2;
12+
T_0 = T_1;
13+
T_1 = T_2;
14+
T_2 = sum;
15+
}
16+
17+
return sum;
18+
}
19+
}

0 commit comments

Comments
 (0)