18 Star 133 Fork 63

编程语言算法集 / C-Sharp

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
ThreeNPlusOneStepsSequence.cs 1.30 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>
/// Number of halving and tripling steps to reach 1 in the '3n+1' problem.
/// </para>
/// <para>
/// Wikipedia: https://en.wikipedia.org/wiki/Collatz_conjecture.
/// </para>
/// <para>
/// OEIS: https://oeis.org/A006577.
/// </para>
/// </summary>
public class ThreeNPlusOneStepsSequence : ISequence
{
/// <summary>
/// Gets sequence of number of halving and tripling steps to reach 1 in the '3n+1' problem.
/// </summary>
public IEnumerable<BigInteger> Sequence
{
get
{
BigInteger startingValue = 1;
while (true)
{
BigInteger counter = 0;
BigInteger currentValue = startingValue;
while (currentValue != 1)
{
if (currentValue.IsEven)
{
currentValue /= 2;
}
else
{
currentValue = 3 * currentValue + 1;
}
counter++;
}
yield return counter;
startingValue++;
}
}
}
}
C#
1
https://gitee.com/TheAlgorithms/C-Sharp.git
git@gitee.com:TheAlgorithms/C-Sharp.git
TheAlgorithms
C-Sharp
C-Sharp
master

搜索帮助