C #, 1923
Mungkin tidak akan menjadi program terpendek tetapi saya menemukan tantangan yang menarik, jadi inilah solusi saya.
Menjalankan semua 4 dengan 35 Angka (15 35) membutuhkan waktu sekitar 5 detik.
Anda dapat mengujinya di sini , tetapi perhatikan bahwa jika Anda ingin OEIS4 jumlah digit yang Anda inginkan harus kecil atau netfiddle kehabisan memori.
Golf
using System;using System.Collections;using System.Collections.Generic;using System.Linq;class p{public static void Main(string[] args){int b=0;IEnumerable<int>a=null;foreach(char c in Convert.ToString(int.Parse(args[0]),2).Reverse()){++b;if(c=='0')continue;switch(b){case 1: a=d(a,e());break;case 2: a=d(a,f());break;case 3: a=d(a,g());break;case 4: a=d(a,h(),true);break;}}if(a==null)return;bool j=true;foreach(int i in a.Take(int.Parse(args[1]))){if(j)j=false;else Console.Write(",");Console.Write(i);}}static IEnumerable<int>d(IEnumerable<int>k,IEnumerable<int>l,bool m=false){if(k==null)foreach(int n in l)yield return n;int o=0;int p=1;foreach(int i in k){Dictionary<int,HashSet<int>>q=m ? new Dictionary<int,HashSet<int>>(): null;int s=0;foreach(int n in l){if(!m){if(i<n)break;}else{if(!q.ContainsKey(o))q.Add(o,new HashSet<int>());q[o].Add(n);if(q.Count==1){int r=q[o].OrderBy(gi =>gi).Take(2).Sum();if(i<r)break;}else{int r=q[o].Concat(q[o-1]).OrderBy(gi =>gi).Take(2).Sum();if(i<r)break;}if(++s==p){o++;p=(int)Math.Pow(2,o);}}if(i==n){yield return i;break;}}}}static IEnumerable<int>e(){int t=0;for(int i=0;i<int.MaxValue;i++)foreach(char c in Convert.ToString(i,2)){if(c=='0')yield return t;t++;}}static IEnumerable<int>f(){int t=1;int u=0;bool v=true;using(IEnumerator<int>w=Enumerable.Range(0,int.MaxValue).GetEnumerator()){while(w.MoveNext()){if(v){if(u==0)u=t+1;yield return w.Current;if(--t==0)v=false;}else{if(t==0)t=u+1;if(--u==0)v=true;}}}}static IEnumerable<int>g(){for(int i=0;i<int.MaxValue;i++){string s=Convert.ToString(i,2);if(x(s.Count(c =>c=='0'))&& x(s.Count(c =>c=='1')))yield return i;}}static bool x(int y){return(y != 0)&&((y &(y-1))==0);}static IEnumerable<int>h(){return Enumerable.Range(1,int.MaxValue).Select(z);}static Dictionary<int,int>_=new Dictionary<int,int>();static int z(int n){int a;if(!_.TryGetValue(n,out a)){if(n<3)a=1;else a=z(n-z(n-1))+z(n-z(n-2));_.Add(n,a);}return a;}}
Dapat dibaca
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
class Programm
{
public static void Main(string[] args)
{
int index = 0;
IEnumerable<int> intersection = null;
foreach (char c in Convert.ToString(int.Parse(args[0]), 2).Reverse())
{
++index;
if (c == '0')
continue;
switch (index)
{
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)
return;
bool first = true;
foreach (int i in intersection.Take(int.Parse(args[1])))
{
if (first) first = false;
else Console.Write(",");
Console.Write(i);
}
Console.ReadKey();
}
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;
int generation = 0;
int generationMax = 1;
foreach (int i in intersection)
{
Dictionary<int, HashSet<int>> generationCache = hof ? new Dictionary<int, HashSet<int>>() : null;
int count = 0;
foreach (int n in newSequence)
{
if (!hof)
{
if (i < n)
break;
}
else
{
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();
if (i < lowerBound)
break;
}
else
{
int lowerBound = generationCache[generation].Concat(generationCache[generation - 1]).OrderBy(gi => gi).Take(2).Sum();
if (i < lowerBound)
break;
}
if (++count == generationMax)
{
generation++;
generationMax = (int)Math.Pow(2, generation);
}
}
if (i == n)
{
yield return i;
break;
}
}
}
}
static IEnumerable<int> OEIS1()
{
int position = 0;
for (int i = 0; i < int.MaxValue; i++)
foreach (char c in Convert.ToString(i, 2))
{
if (c == '0')
yield return position;
position++;
}
}
static IEnumerable<int> OEIS2()
{
int take = 1;
int skip = 0;
bool doTake = true;
using (IEnumerator<int> enumerator = Enumerable.Range(0, int.MaxValue).GetEnumerator())
{
while (enumerator.MoveNext())
{
if (doTake)
{
if (skip == 0)
skip = take + 1;
yield return enumerator.Current;
if (--take == 0)
doTake = false;
}
else
{
if (take == 0)
take = skip + 1;
if (--skip == 0)
doTake = true;
}
}
}
}
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')))
yield return i;
}
}
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)
{
int result;
if (!_hofstadterQCache.TryGetValue(n, out result))
{
if (n < 3)
result = 1;
else
result = HofstadterQ(n - HofstadterQ(n - 1)) + HofstadterQ(n - HofstadterQ(n - 2));
_hofstadterQCache.Add(n, result);
}
return result;
}
}
Penjelasan
Ini menggunakan evaluasi malas malas yang membuatnya cepat cepat saya percaya. Juga saya malas melakukan "bitlogic" apa pun dengan menggunakan kerangka kerja Convert.ToString (angka, 2) metode. Ini mengubah angka apa pun menjadi representasi binray sebagai string.
Saya harus menulis metode saya sendiri untuk memotong seuqences karena metode Linq memotong persimpangan dari urutan penuh, dan itu benar-benar mustahil.
12 5
contoh hingga indeks yang sama, maka10
apakah memang datang sebelumnya9
di persimpangan ... seperti, bagaimana Anda, saat akan melalui urutan, memutuskan apakah akan melewatkan9
di # 3 sebagai persimpangan mungkin? Seperti jika # 3 ada7
di dalamnya maka Anda akan diminta untuk melewatinya karena itu tidak muncul di # 4