18 Star 133 Fork 63

编程语言算法集 / C-Sharp

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
OnesCountingSequence.cs 3.20 KB
一键复制 编辑 原始数据 按行查看 历史
Richard Vasquez 提交于 2022-10-05 11:23 . Add A000120 (#340)
using System.Collections.Generic;
using System.Numerics;
namespace Algorithms.Sequences;
/// <summary>
/// <para>
/// 1's-counting sequence: number of 1's in binary expression of n.
/// </para>
/// <para>
/// OEIS: https://oeis.org/A000120.
/// </para>
/// </summary>
public class OnesCountingSequence : ISequence
{
/// <summary>
/// <para>
/// Gets the generated sequence of the 1's contained in the binary representation of n.
/// </para>
/// <para>
/// The sequence is generated as follows.
/// 1. The initial 0 value is provided.
/// 2. A recursively generated sequence is iterated, starting with a length of 1 (i.e., 2^0),
/// followed by increasing 2^x length values.
/// 3. Each sequence starts with the value 1, and a targeted value of depths that it will recurse
/// for the specific iteration.
/// 4. If the call depth to the recursive function is met, it returns the value argument received.
/// 5. If the call depth has not been met, it recurses to create 2 sequences, one starting with the
/// value argument, and the following with the value argument + 1.
/// 6. Using ':' as a visual separator for each sequence, this results in the following sequences
/// that are returned sequentially after the initial 0.
/// 1 : 1, 2 : 1, 2, 2, 3 : 1, 2, 2, 3, 2, 3, 3, 4.
/// </para>
/// <remarks>
/// <para>
/// This one comes from thinking over information contained within the COMMENTS section of the OEIS page.
/// </para>
/// <para>
/// Using the comments provided by Benoit Cloitre, Robert G. Wilson v, and Daniel Forgues, the above
/// algorithm was coded.
/// </para>
/// </remarks>
/// </summary>
public IEnumerable<BigInteger> Sequence
{
get
{
yield return 0;
var depth = 0;
while (true)
{
foreach (var count in GenerateFractalCount(BigInteger.One, depth))
{
yield return count;
}
depth++;
}
}
}
/// <summary>
/// <para>
/// Recursive function to generate sequences.
/// </para>
/// </summary>
/// <param name="i">The value that will start off the current IEnumerable sequence.</param>
/// <param name="depth">The remaining depth of recursion. Value of 0 is the stop condition.</param>
/// <returns>An IEnumerable sequence of BigInteger values that can be iterated over.</returns>
private static IEnumerable<BigInteger> GenerateFractalCount(BigInteger i, int depth)
{
// Terminal condition
if (depth == 0)
{
yield return i;
yield break;
}
foreach (var firstHalf in GenerateFractalCount(i, depth - 1))
{
yield return firstHalf;
}
foreach (var secondHalf in GenerateFractalCount(i + 1, depth - 1))
{
yield return secondHalf;
}
}
}
C#
1
https://gitee.com/TheAlgorithms/C-Sharp.git
git@gitee.com:TheAlgorithms/C-Sharp.git
TheAlgorithms
C-Sharp
C-Sharp
master

搜索帮助