18 Star 133 Fork 63

编程语言算法集 / C-Sharp

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
LucasNumbersBeginningAt2Sequence.cs 2.04 KB
一键复制 编辑 原始数据 按行查看 历史
Gerson Jr 提交于 2024-01-03 22:51 . Switch to file-scoped namespaces (#432)
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;
}
}
}
}
C#
1
https://gitee.com/TheAlgorithms/C-Sharp.git
git@gitee.com:TheAlgorithms/C-Sharp.git
TheAlgorithms
C-Sharp
C-Sharp
master

搜索帮助

53164aa7 5694891 3bd8fe86 5694891