-
Notifications
You must be signed in to change notification settings - Fork 1.5k
/
LucasNumbersBeginningAt2Sequence.cs
64 lines (61 loc) · 2.04 KB
/
LucasNumbersBeginningAt2Sequence.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
60
61
62
63
64
using System.Collections.Generic;
using System.Numerics;
namespace Algorithms.Sequences;
/// <summary>
/// <para>
/// Sequence of Lucas number values.
/// </para>
/// <para>
/// Wikipedia: https://en.wikipedia.org/wiki/Lucas_number.
/// </para>
/// <para>
/// OEIS: http://oeis.org/A000032.
/// </para>
/// </summary>
public class LucasNumbersBeginningAt2Sequence : ISequence
{
/// <summary>
/// <para>
/// Gets Lucas number sequence.
/// </para>
/// <para>
/// Lucas numbers follow the same type of operation that the Fibonacci (A000045)
/// sequence performs with starting values of 2, 1 versus 0,1. As Fibonacci does,
/// the ratio between two consecutive Lucas numbers converges to phi.
/// </para>
/// <para>
/// This implementation is similar to A000204, but starts with the index of 0, thus having the
/// initial values being (2,1) instead of starting at index 1 with initial values of (1,3).
/// </para>
/// <para>
/// A simple relationship to Fibonacci can be shown with L(n) = F(n-1) + F(n+1), n>= 1.
///
/// n | L(n) | F(n-1) | F(n+1)
/// --|-------|--------+--------+
/// 0 | 2 | | |
/// 1 | 1 | 0 | 1 |
/// 2 | 3 | 1 | 2 |
/// 3 | 4 | 1 | 3 |
/// 4 | 7 | 2 | 5 |
/// 5 | 11 | 3 | 8 |
/// --|-------|--------+--------+.
/// </para>
/// </summary>
public IEnumerable<BigInteger> Sequence
{
get
{
yield return 2;
yield return 1;
BigInteger previous = 2;
BigInteger current = 1;
while (true)
{
var next = previous + current;
previous = current;
current = next;
yield return next;
}
}
}
}