-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathtribonacci.cs
More file actions
39 lines (35 loc) · 831 Bytes
/
Copy pathtribonacci.cs
File metadata and controls
39 lines (35 loc) · 831 Bytes
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
using System;
public class Program
{
#pragma warning disable
public static int Tribonacci(int n) {
/*
Answer <= 2^31 - 1; that's mean the length of array limit at 41.
Because a parameter is Integer(in C# is Int32).
*/
int[] arr = new int[41];
int answer = 0;
if (n == 0) return 0;
if (n == 1) return 1;
if (n == 2) return 1;
if (arr.Length > 38) {
return 0;
}
for (int i = 0; i < n + 1; i++) {
if (i == 0 || i== 1 || i == 2) {
arr[0] = 0;
arr[1] = 1;
arr[2] = 1;
}
arr[i + 3] = arr[i] + arr[i + 1] + arr[i + 2];
answer = arr[i];
//Console.WriteLine("+++");
//Console.WriteLine(answer);
}
return answer;
}
public static void Main()
{
int answer = Tribonacci(4);
}
}