Created
May 15, 2016 03:46
-
-
Save KirillOsenkov/75edba5ffb169b43fd149a63572320e9 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Collections.Generic; | |
using System.Linq; | |
class Program | |
{ | |
static void Main(string[] args) | |
{ | |
foreach (var item in GetSequence().Take(50)) | |
{ | |
Console.Write(item.ToString() + ", "); | |
} | |
} | |
private static IEnumerable<int> GetSequence() | |
{ | |
var queue = new Queue<int>(); | |
int current = 1; | |
while (true) | |
{ | |
yield return current; | |
queue.Enqueue(current); | |
var currentRun = queue.Dequeue(); | |
if (currentRun == 2) | |
{ | |
yield return current; | |
queue.Enqueue(current); | |
} | |
current = 3 - current; | |
} | |
} | |
} |
Author
KirillOsenkov
commented
May 15, 2016
1, 22, 11, 2, 1, 22, 1, 22, 11, 2, 11, 22, 1, 2, 11, 2, 1, 22, 11, 2, 11, 2, 1, 22, 1, 22, 11, 2, 1, 22, 1, 2, 11
Length of the queue grows as O(n/3) (i.e. for 1000th sequence element the queue length is 333)
As the sequence grows, numbers of 1, 2, 11 and 22 all grow approximately equally
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment