forked from Ramon-Balaguer/Refactoring-Golf
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathFibonacciIndex.cs
39 lines (35 loc) · 1009 Bytes
/
FibonacciIndex.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
using System.Collections.Generic;
namespace Round1.B
{
public class FibonacciIndex
{
public int FindIndexOf(long fibonacci)
{
if(fibonacci >= 0 && fibonacci < 2){
return (int)fibonacci;
}
return Seek(fibonacci);
}
private int Seek(long fibonacci)
{
int indexOfFibonacci = -1;
int currentIndex = 2;
long f = 0;
var sequence = BuildInitialSequence();
while (f < fibonacci)
{
f = sequence[currentIndex - 1] + sequence[currentIndex - 2];
if (f == fibonacci)
indexOfFibonacci = currentIndex;
sequence.Add(f);
currentIndex++;
}
return indexOfFibonacci;
}
private List<long> BuildInitialSequence()
{
List<long> sequence = new List<long> {0L, 1L};
return sequence;
}
}
}