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 57ea7f1

Browse files
n-th-tribonacci-number
1 parent a857fae commit 57ea7f1

File tree

2 files changed

+54
-0
lines changed

2 files changed

+54
-0
lines changed
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
/**
2+
* @param {number} n
3+
* @return {number}
4+
*/
5+
var tribonacci = function (n) {
6+
let first = 0, second = 0, third = 1;
7+
let result = 0;
8+
n = n+1;
9+
if (n == 0) return first;
10+
if (n == 1) return second;
11+
if (n == 2) return third;
12+
13+
while (n > 2) {
14+
result = first + second + third;
15+
first = second;
16+
second = third;
17+
third = result;
18+
n--;
19+
}
20+
return result;
21+
};
22+
23+
console.log(tribonacci(3))
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
# N-th Tribonacci Number
2+
3+
The Tribonacci sequence Tn is defined as follows:
4+
5+
T0 = 0, T1 = 1, T2 = 1, and Tn+3 = Tn + Tn+1 + Tn+2 for n >= 0.
6+
7+
Given n, return the value of Tn.
8+
9+
## Example 1:=
10+
11+
Input: n = 4
12+
13+
Output: 4
14+
15+
Explanation:
16+
17+
T_3 = 0 +たす 1 +たす 1 = 2
18+
19+
T_4 = 1 +たす 1 +たす 2 = 4
20+
21+
## Example 2
22+
23+
Input: n = 25
24+
25+
Output: 1389537
26+
27+
## Constraints
28+
29+
0 <= n <= 37
30+
31+
The answer is guaranteed to fit within a 32-bit integer, ie. answer <= 2^31 - 1.'

0 commit comments

Comments
(0)

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