forked from TheAlgorithms/C-Sharp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathKolakoskiSequence2.cs
45 lines (41 loc) · 1.1 KB
/
KolakoskiSequence2.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
using System.Collections.Generic;
using System.Linq;
using System.Numerics;
namespace Algorithms.Sequences;
/// <summary>
/// <para>
/// Kolakoski sequence; n-th element is the length of the n-th run in the sequence itself.
/// </para>
/// <para>
/// Wikipedia: https://en.wikipedia.org/wiki/Kolakoski_sequence.
/// </para>
/// <para>
/// OEIS: https://oeis.org/A000002.
/// </para>
/// </summary>
public class KolakoskiSequence2 : ISequence
{
/// <summary>
/// Gets Kolakoski sequence.
/// </summary>
public IEnumerable<BigInteger> Sequence
{
get
{
yield return 1;
yield return 2;
yield return 2;
var inner = new KolakoskiSequence2().Sequence.Skip(2);
var nextElement = 1;
foreach (var runLength in inner)
{
yield return nextElement;
if (runLength > 1)
{
yield return nextElement;
}
nextElement = 1 + nextElement % 2;
}
}
}
}