18 Star 133 Fork 63

编程语言算法集 / C-Sharp

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
VanEcksSequence.cs 1.21 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>
/// Van Eck's sequence. For n >= 1, if there exists an m &lt; n such that a(m) = a(n), take the largest such m and set a(n+1) = n-m; otherwise a(n+1) = 0. Start with a(1)=0.
/// </para>
/// <para>
/// OEIS: http://oeis.org/A181391.
/// </para>
/// </summary>
public class VanEcksSequence : ISequence
{
/// <summary>
/// Gets Van Eck's sequence.
/// </summary>
public IEnumerable<BigInteger> Sequence
{
get
{
yield return 0;
var dictionary = new Dictionary<BigInteger, BigInteger>();
BigInteger previous = 0;
BigInteger currentIndex = 2; // 1-based index
while (true)
{
BigInteger element = 0;
if (dictionary.TryGetValue(previous, out var previousIndex))
{
element = currentIndex - previousIndex;
}
yield return element;
dictionary[previous] = currentIndex;
previous = element;
currentIndex++;
}
}
}
}
C#
1
https://gitee.com/TheAlgorithms/C-Sharp.git
git@gitee.com:TheAlgorithms/C-Sharp.git
TheAlgorithms
C-Sharp
C-Sharp
master

搜索帮助