forked from TheAlgorithms/C-Sharp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
OnesCountingSequence.cs
89 lines (83 loc) · 3.2 KB
/
OnesCountingSequence.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
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;
}
}
}