入力nを指定して、n番目のNarayana-Zidek-Capell数を生成します。少ないバイトが勝ちます。
f(1)= 1、f(n)は前のfloor(n / 2)ナラヤナ-ジデック-カペル項の合計です。
テストケース:
f(1)=1
f(9)=42
f(14)=1308
f(15)=2605
f(23)=664299
入力nを指定して、n番目のNarayana-Zidek-Capell数を生成します。少ないバイトが勝ちます。
f(1)= 1、f(n)は前のfloor(n / 2)ナラヤナ-ジデック-カペル項の合計です。
テストケース:
f(1)=1
f(9)=42
f(14)=1308
f(15)=2605
f(23)=664299
回答:
HĊrµṖ߀Sȯ1
n
引数として取り、結果を出力します。
H divide input by 2
Ċ round up to get first n to recurse
r inclusive range from that to n
µ (chain separator)
Ṗ remove n itself from the range
߀ call self recursively on each value in the range
S sum results
ȯ1 if sum was zero, return one
05AB1Eとしての反復ソリューションには機能がありません。
X¸sGDN>;ï£Os‚˜}¬
X¸ # initialize a list with 1
sG } # input-1 number of times do
D # duplicate current list
N>;ï£ # take n/2 elements from the list
O # sum those elements
s‚˜ # add at the start of the list
¬ # get the first element and implicitly print
OEISアルゴリズムの翻訳。この辺りには十分なCコードがありません!
f(n){return n<3?:2*f(n-1)-n%2*f(n/2);}
n<3?:(...)
ますか?
def f(n):
x=1,
for i in range(n):x+=sum(x[-i//2:]),
print(x[-1])
引数を介して入力を受け取り、STDOUTに出力する関数。これは、定義の直接の実装です。
使い方
def f(n): Function with input target term index n
x=1, Initialise term list x as tuple (1)
for i in range(n):... For all term indices in [0,n-1]...
x[-i//2:] ..yield the previous floor(i/2) terms...
x+=sum(...) ...and append their sum to x
print(x[-1]) Print the last term in x, which is the nth term
If[#<4,1,2#0[#-1]-#~Mod~2#0[(#-1)/2]]&
匿名関数。入力としてTakeを受け取り、出力として𝑓(𝑛)を返します。Rubyソリューションに基づいています。
int z(int n){return n<3?1:n%2>0?(2*z(n-1)-z(n/2)):(2*z(n-1));}
これは再帰のない完全なプログラムです。再帰関数は52バイトで定義できます(それを打つことは可能かもしれません)が、それはsherlock9の答えのかなり退屈なポートです(そしてf(100)以上を要求するとエラーになります)ので、私はこれを立てていますより長く、より興味深いバージョン
<?php for($i=$argv[1];$j=$i;$i--)for(;--$j*2>=$i;)$a[$j]+=$a[$i]?:1;echo$a[1]?:1;
多くの(O [n])通知を引き起こしますが、それで問題ありません。
O(n)
通知?え?
f=n=>Math.round(n<3?1:2*f(n-1)-n%2*f(parseInt(n/2)))
Cの回答に基づきます。
parseInt
代わりに使用して2バイトを保存しましたMath.floor
f=function(n,a=0)if(n<2)1 else{for(i in n-1:(n%/%2))a=a+f(i);a}
a=0
2つの中かっこが保存されるため、デフォルトとして追加されます。関数は必要に応じて再帰的に自分自身を呼び出します。