-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFibo.cs
59 lines (52 loc) · 1.1 KB
/
Fibo.cs
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
50
51
52
53
54
55
56
57
58
59
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Numerics;
namespace Fibo
{
class Fibo : ITask
{
public string Run(string[] data)
{
int N = Convert.ToInt32(data[0]);
// long answer = FindRecursive(N);
BigInteger answer = FindIterative(N);
return answer.ToString();
}
public long FindRecursive(long N)
{
long result = 0;
if(N == 0)
result = 0;
else if((N == 1) || (N == 2))
result = 1;
else
result = FindRecursive(N - 1) + FindRecursive(N - 2);
return result;
}
public static BigInteger FindIterative(long N)
{
BigInteger result = 0;
if(N == 0)
result = 0;
else if((N == 1) || (N == 2))
result = 1;
else
{
BigInteger F1 = 1;
BigInteger F2 = 1;
BigInteger FN = 0;
for(long i=2; i<N; i++)
{
FN = F1+F2;
F1 = F2;
F2 = FN;
}
result = FN;
}
return result;
}
}
}