1 Star 0 Fork 0

jobily/TheAlgorithms-C-Sharp

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
RecamansSequence.cs 1.18 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>
/// Recaman's sequence. a(0) = 0; for n > 0, a(n) = a(n-1) - n if nonnegative and not already in the sequence, otherwise a(n) = a(n-1) + n.
/// </para>
/// <para>
/// Wikipedia: https://en.wikipedia.org/wiki/Recam%C3%A1n%27s_sequence.
/// </para>
/// <para>
/// OEIS: http://oeis.org/A005132.
/// </para>
/// </summary>
public class RecamansSequence : ISequence
{
/// <summary>
/// Gets Recaman's sequence.
/// </summary>
public IEnumerable<BigInteger> Sequence
{
get
{
yield return 0;
var elements = new HashSet<BigInteger> { 0 };
var previous = 0;
var i = 1;
while (true)
{
var current = previous - i;
if (current < 0 || elements.Contains(current))
{
current = previous + i;
}
yield return current;
previous = current;
elements.Add(current);
i++;
}
}
}
}
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/hubo/the-algorithms-c-sharp.git
git@gitee.com:hubo/the-algorithms-c-sharp.git
hubo
the-algorithms-c-sharp
TheAlgorithms-C-Sharp
master

搜索帮助

23e8dbc6 1850385 7e0993f3 1850385