18 Star 133 Fork 63

编程语言算法集 / 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++;
}
}
}
}
C#
1
https://gitee.com/TheAlgorithms/C-Sharp.git
git@gitee.com:TheAlgorithms/C-Sharp.git
TheAlgorithms
C-Sharp
C-Sharp
master

搜索帮助