Skip to content

Instantly share code, notes, and snippets.

@KirillOsenkov
Created May 15, 2016 03:46
Show Gist options
  • Save KirillOsenkov/75edba5ffb169b43fd149a63572320e9 to your computer and use it in GitHub Desktop.
Save KirillOsenkov/75edba5ffb169b43fd149a63572320e9 to your computer and use it in GitHub Desktop.
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;
}
}
}
@KirillOsenkov
Copy link
Author

image

@KirillOsenkov
Copy link
Author

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

@KirillOsenkov
Copy link
Author

Length of the queue grows as O(n/3) (i.e. for 1000th sequence element the queue length is 333)

@KirillOsenkov
Copy link
Author

As the sequence grows, numbers of 1, 2, 11 and 22 all grow approximately equally

@KirillOsenkov
Copy link
Author

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment