forked from TheAlgorithms/C-Sharp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
FibonacciSequence.cs
40 lines (39 loc) · 999 Bytes
/
FibonacciSequence.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
using System.Collections.Generic;
using System.Numerics;
namespace Algorithms.Sequences
{
/// <summary>
/// <para>
/// Fibonacci sequence.
/// </para>
/// <para>
/// Wikipedia: https://wikipedia.org/wiki/Fibonacci_number.
/// </para>
/// <para>
/// OEIS: https://oeis.org/A000045.
/// </para>
/// </summary>
public class FibonacciSequence : ISequence
{
/// <summary>
/// Gets Fibonacci sequence.
/// </summary>
public IEnumerable<BigInteger> Sequence
{
get
{
yield return 0;
yield return 1;
BigInteger previous = 0;
BigInteger current = 1;
while (true)
{
var next = previous + current;
previous = current;
current = next;
yield return next;
}
}
}
}
}