forked from idg10/prog-cs-8-examples
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFibonacciEnumerable.cs
48 lines (39 loc) · 1.01 KB
/
FibonacciEnumerable.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
using System.Collections;
using System.Collections.Generic;
using System.Numerics;
namespace ImplementingSequences
{
public class FibonacciEnumerable :
IEnumerable<BigInteger>, IEnumerator<BigInteger>
{
private BigInteger v1;
private BigInteger v2;
private bool first = true;
public BigInteger Current => v1;
public void Dispose() { }
object IEnumerator.Current => Current;
public bool MoveNext()
{
if (first)
{
v1 = 1;
v2 = 1;
first = false;
}
else
{
var tmp = v2;
v2 = v1 + v2;
v1 = tmp;
}
return true;
}
public void Reset()
{
first = true;
}
public IEnumerator<BigInteger> GetEnumerator() =>
new FibonacciEnumerable();
IEnumerator IEnumerable.GetEnumerator() => GetEnumerator();
}
}