代码拉取完成,页面将自动刷新
using System.Collections.Generic;
using System.Numerics;
namespace Algorithms.Sequences;
/// <summary>
/// <para>
/// Van Eck's sequence. For n >= 1, if there exists an m < 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++;
}
}
}
}
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。