18 Star 133 Fork 63

编程语言算法集 / C-Sharp

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
KolakoskiSequence.cs 1.13 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>
/// Kolakoski sequence; n-th element is the length of the n-th run in the sequence itself.
/// </para>
/// <para>
/// Wikipedia: https://en.wikipedia.org/wiki/Kolakoski_sequence.
/// </para>
/// <para>
/// OEIS: https://oeis.org/A000002.
/// </para>
/// </summary>
public class KolakoskiSequence : ISequence
{
/// <summary>
/// Gets Kolakoski sequence.
/// </summary>
public IEnumerable<BigInteger> Sequence
{
get
{
yield return 1;
yield return 2;
yield return 2;
var queue = new Queue<int>();
queue.Enqueue(2);
var nextElement = 1;
while (true)
{
var nextRun = queue.Dequeue();
for (var i = 0; i < nextRun; i++)
{
queue.Enqueue(nextElement);
yield return nextElement;
}
nextElement = 1 + nextElement % 2;
}
}
}
}
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