using System.Collections;
using System.Collections.Generic;
public static void Main(string[] args)
IEnumerable<int> intersection = null;
foreach (char c in Convert.ToString(mode, 2).Reverse())
case 1: intersection = _join(intersection, OEIS1()); break;
case 2: intersection = _join(intersection, OEIS2()); break;
case 3: intersection = _join(intersection, OEIS3()); break;
case 4: intersection = _join(intersection, OEIS4(), true); break;
default: throw new ArgumentException();
if (intersection == null)
foreach (int i in intersection.Take(take))
if (first) first = false;
private static IEnumerable<int> _join(IEnumerable<int> intersection, IEnumerable<int> newSequence, bool hof = false)
if (intersection == null)
foreach (int n in newSequence) yield return n;
foreach (int i in intersection)
Dictionary<int, HashSet<int>> generationCache = hof ? new Dictionary<int, HashSet<int>>() : null;
foreach (int n in newSequence)
if (!generationCache.ContainsKey(generation))
generationCache.Add(generation, new HashSet<int>());
generationCache[generation].Add(n);
if (generationCache.Count == 1)
int lowerBound = generationCache[generation].OrderBy(gi => gi).Take(2).Sum();
int lowerBound = generationCache[generation].Concat(generationCache[generation - 1]).OrderBy(gi => gi).Take(2).Sum();
if (++count == generationMax)
generationMax = (int)Math.Pow(2, generation);
static IEnumerable<int> OEIS1()
for (int i = 0; i < int.MaxValue; i++)
foreach (char c in Convert.ToString(i, 2))
static IEnumerable<int> OEIS2()
using (IEnumerator<int> enumerator = Enumerable.Range(0, int.MaxValue).GetEnumerator())
while (enumerator.MoveNext())
yield return enumerator.Current;
static IEnumerable<int> OEIS3()
for (int i = 0; i < int.MaxValue; i++)
string s = Convert.ToString(i, 2);
if (_isPowerOfTwo(s.Count(c => c == '0')) && _isPowerOfTwo(s.Count(c => c == '1')))
static bool _isPowerOfTwo(int number)
return (number != 0) && ((number & (number - 1)) == 0);
static IEnumerable<int> OEIS4()
return Enumerable.Range(1, int.MaxValue).Select(HofstadterQ);
static Dictionary<int, int> _hofstadterQCache = new Dictionary<int, int>();
static int HofstadterQ(int n)
if (!_hofstadterQCache.TryGetValue(n, out result))
result = HofstadterQ(n - HofstadterQ(n - 1)) + HofstadterQ(n - HofstadterQ(n - 2));
_hofstadterQCache.Add(n, result);