-
Notifications
You must be signed in to change notification settings - Fork 1.5k
/
GolombsSequence.cs
44 lines (40 loc) · 1.06 KB
/
GolombsSequence.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
using System.Collections.Generic;
using System.Numerics;
namespace Algorithms.Sequences;
/// <summary>
/// <para>
/// Golomb's sequence. a(n) is the number of times n occurs in the sequence, starting with a(1) = 1.
/// </para>
/// <para>
/// Wikipedia: https://en.wikipedia.org/wiki/Golomb_sequence.
/// </para>
/// <para>
/// OEIS: https://oeis.org/A001462.
/// </para>
/// </summary>
public class GolombsSequence : ISequence
{
/// <summary>
/// Gets Golomb's sequence.
/// </summary>
public IEnumerable<BigInteger> Sequence
{
get
{
yield return 1;
yield return 2;
yield return 2;
var queue = new Queue<BigInteger>();
queue.Enqueue(2);
for (var i = 3; ; i++)
{
var repetitions = queue.Dequeue();
for (var j = 0; j < repetitions; j++)
{
queue.Enqueue(i);
yield return i;
}
}
}
}
}