Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit bd7fefa

Browse files
1137. N-th Tribonacci Number
1 parent 74c4d57 commit bd7fefa

File tree

1 file changed

+31
-0
lines changed

1 file changed

+31
-0
lines changed

‎1137. N-th Tribonacci Number.kt‎

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
/**
2+
The tribonacci series is a generalization of the Fibonacci sequence where each term is the sum of the three preceding terms.
3+
The Tribonacci Sequence:
4+
0, 0, 1, 1, 2, 4, 7, 13, 24, 44, 81, 149, 274, 504, 927, 1705, 3136, 5768, 10609, 19513, 35890, 66012, 121415, 223317, 410744, 755476, 1389537, 2555757, 4700770, 8646064, 15902591, 29249425, 53798080, 98950096, 181997601, 334745777, 615693474, 1132436852... so on
5+
General Form of Tribonacci number:
6+
7+
8+
a(n) = a(n-1) + a(n-2) + a(n-3)
9+
with
10+
a(0) = a(1) = 0, a(2) = 1.
11+
*/
12+
class Solution {
13+
fun tribonacci(n: Int): Int {
14+
if(n == 0){
15+
return 0
16+
}
17+
if(n==1 || n == 2) return 1
18+
19+
var a = 0 //T_0
20+
var b = 1 //T_1
21+
var c = 1 //T_2
22+
23+
for(i in 3..n){
24+
val next = a + b + c
25+
a = b
26+
b = c
27+
c = next
28+
}
29+
return c
30+
}
31+
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /