あなたの言語には最大再帰深度(MRD)がありますか?
あなたの言語がMRD = 500を持っているとしましょう
再帰の深さを見つけて正確な値を出力するコードを書く
上記の場合、プログラム(または関数)は500を出力するはずです
Code-Golf 最短回答が勝ちます!
あなたの言語には最大再帰深度(MRD)がありますか?
あなたの言語がMRD = 500を持っているとしましょう
再帰の深さを見つけて正確な値を出力するコードを書く
上記の場合、プログラム(または関数)は500を出力するはずです
Code-Golf 最短回答が勝ちます!
回答:
def f(x=2):
try:f(x+1)
except:print(x)
ビルトインから読むだけではありません。except節はエラーの1レベル前に実行されるため、1ではなく2から始めます。もちろん、これはPython 2では1バイト短くなっています。
f=_=>do{try{-~f()}catch(e){}}
ここでそれを試してみてください、またはでそれをテストするには、以下のスニペットを使うeval代わりにdo。
console.log((f=_=>eval(`try{-~f()}catch(e){}`))())
本質的には同一であるため、これを個別のソリューションとして投稿する価値はありません。
Ox`try\{-~rp()}¯t®(e)\{}
JavaScript自体には再帰制限はありません。むしろ、インタープリター(つまり、ブラウザー)によって制限が課されます-インタープリターによって言語を定義するのは良いことです。数ある要因の中でも、制限はブラウザーと使用可能なメモリによって異なり、実行される操作の影響を受けます。次のスニペットは、私が経験したこのソリューションの5つの異なるバージョンを使用して、最後のポイントを示しています。最後の2つのテストからわかるように、Chromeでは、少なくとも操作の順序でさえ違いを生むことができます。
console.log((f=(i=0)=>eval(`try{f(i+1)}catch(e){i}`))())
console.log((f=i=>eval(`try{f(-~i)}catch(e){i}`))())
console.log((f=(i=0)=>eval(`try{f(++i)}catch(e){i}`))())
console.log((f=_=>eval(`try{-~f()}catch(e){}`))())
console.log((f=_=>eval(`try{f()+1}catch(e){0}`))())
console.log((f=_=>eval(`try{1+f()}catch(e){0}`))())
したがって、定数またはメソッドを使用する便利さはありません。その代わりに、最終的には、最終的には自分自身を継続的に呼び出す関数を作成します。最も単純な形式は次のとおりです。
f=_=>f()
しかし、それは、f自分自身が何回呼び出されたかを示すことなく、オーバーフローエラーをスローするだけなので、このチャレンジではあまり役に立ちません。エラーを回避tryするには、f連続して呼び出しcatch、失敗したときに呼び出します。
f=_=>{try{f()}catch(e){}}
エラーはありませんが、catch実際には何も実行していないため、失敗する前に関数がそれ自体を呼び出した回数の戻り値はありません。try / catchステートメントを評価してみましょう:
f=_=>eval(`try{f()}catch(e){}`)
これで値が返されます(これはコードゴルフであるため、実際のを使用するよりも数バイト節約できましたreturn)。ただし、返される値はであり、undefinedこれもcatchが何も実行していないためです。幸いにも私たちのために-~undefined==1と-~n==n+1そう、ポップで-~の呼び出しの前にf、我々は、本質的に持っている-~-~ ... -~-~undefined他に、-~回数が私たちを与えて、各呼び出しで先頭に付加fと呼ばれていました。
f=_=>eval(`try{-~f()}catch(e){}`)
f=_=>eval('try{-~f()}catch(e){}')
#0[#+1];&@1
%[[1,1]]
;計算を省略します1+$IterationLimit(おそらくMathematicaが関数をテール最適化するため)。または、のデフォルト、つまり(上記の両方の値よりも大きい)を0 //. x_ -> x + 1計算しReplaceRepeatedます。MaxIteration65536
(これは結果を評価するコードスニペットです。しかし、他のMathematicaソリューションもそうです)
1+$: ::]
そのため、入力なしで動詞を実行する方法が実際にはわからず、いくつかの簡単な検索(および個人的な直感)が不可能なように見えます。その場合は、その方法を教えてください。回答を削除または更新します。ただし、動詞に何も入力しないと意味がありません。これに照らして、与えられた関数は0、整数のデフォルトの「空の」入力であるexpectsを想定しています。おそらく空の配列を使用するように変更できます(0$0もっとふさわしいと思うなら)ます。
編集:OPは、関数が0を取ることを許可しました。
1+$: ::]
::] Assign adverse: if an error occurs, call ] (the identify function)
1+ Add one to
$: Recursive call to self
これは自分自身を再帰的に呼び出し、スタックエラーが発生するまで入力に1を追加します(0が期待されます)。エラーが発生すると]、入力に対して逆(-right identity)を呼び出しますが、これは0です。
By the way, the space is necessary.
(1+$: ::]) 0
import sys
sys.getrecursionlimit
Saved 9 bytes thanks to @FryAmTheEggman!
from sys import*
getrecursionlimit
__import__('sys').getrecursionlimit
The last 2 are thanks to @totallyhuman
-4 bytes thanks to @scottinet
c;f(){f(++c);}h(){exit(printf("%d",c));}main(){int b[512];f(sigaction(11,(int*[]){h,[17]=1<<27},sigaltstack((int*[]){b,0,2048},0)));}
Installs a SIGSEGV (signal 11) handler with an alternate signal stack (minimum size MINSIGSTKSZ is 2 KB, flag SA_ONSTACK is 0x08000000), then calls a function without arguments and no local variables recursively until the stack overflows. It's interesting that the maximum recursion depth varies across runs, probably due to ASLR.
The maximum recursion depth in C depends on a lot of factors, of course. On a typical 64-bit Linux system the default stack size is 8 MB, and the stack alignment is 16 bytes, so you get a recursion depth of about 512K for simple functions.
Also note that the program above doesn't work with -O2 because of tail call optimization.
int d;int c(){try{c();}finally{return++d;}}
-80 bytes thanks to @Nevay. I tried a method instead of program as well, but made a mistake so ended up with a full program.. Now it's a method.
-3 bytes thanks to @Neil by making use of finally instead of catch(Error e).
-5 byte thanks to @Nevay again.
Explanation:
int d; // Depth-integer `d` on class-level (implicit 0)
int c(){ // Method without parameter and integer return-type
try{c();} // Recursive call
finally{return++d;} // Increase depth-integer `d` and always return it,
// whether a StackOverflowError occurs or not
} // End of method
int c(){try{return-~c();}catch(Error e){return 1;}}
int c(){int n=1;try{n=-~c();}finally{return n;}} saves 3 bytes but gives me a different answer?
int c(){int n=1;try{n+=c();}finally{return n;}}
int d;int c(){try{c();}finally{return++d;}}
-8 bytes thanks to Sven Hohenstein : $ will do partial matching, so we can just use exp instead of the full expressions.
cat(options()$exp)
The options command can also be used to set the recursion depth, i.e., options(expressions=500) for 500.
ressions due to partial matching with $.
2 bytes removed thanks to a suggestion by Sanchises
@max_recursion_depth
Anonymous function that outputs the value.
(), as max_recursion_depth is also a function.
@ to keep it distinct (defining a function rather than REPL'ing the result).
disp (I would have included it, but that's my personal opinion on Octave REPL, and I am not sure of any meta consensus on that)
f(){f $[++i];f};set -x;f
Try it online! (See under debug)
f(){ f $[++i];};set -x;f
Try it online! (See under debug)
f(){ f $(($1+1));};set -x;f
Try it online! (See under debug)
f(){ f $(($1+1));};set -x;f
Try it online! (Exceeds tio debug output, run it in your own shell)
i=0 and the echo not be included in your byte count?
f in pcall.
-- you can confirm that it is still a recursive call with no optimizations
Solution:
{@[.z.s;x+1;x]}0
Example:
/ solution
q){@[.z.s;x+1;x]}0
2000
/ without apply (try/catch)
q){.z.s x+1}0
'stack
@
{.z.s x+1}
2001
Explanation:
Try to recurse, increase x by one each time, if error, return x.
{@[.z.s;x+1;x]}0 / the solution
{ }0 / call lambda function with 0
@[ ; ; ] / @[function;argument;catch]
.z.s / call self (ie recurse)
x+1 / increment x
x / return x if function returns error
?Application.MaxIterations
Not recursion depth per-se, this actually outputs the maximum number of iterations for a cell in an Excel worksheet. Given that the output pertains to a language other than the language in which this is written, perhaps this is more appropriate:
Function f:f=Application.MaxIterations
As that can be called from a cell with
=f(
For VBA with no built in:
-9 as variable no longer needs to be initialised or printed
-4 as code execution no longer has to be ended to avoid multiple prints
Sub s:[A1]=[A1]+1:On Error Resume Next:s
Call with s in the immediate window, outputs to cell A1 of the worksheet
(warning takes a while to run now, add Application.ScreenUpdating = False first)
x=2
f=load"x=x+1;f()"pcall(f)print(x)
I don't know which value to initialize x with as I don't know the number of intermediary calls there are...
-23 bytes by getting rid of the atom
-7 bytes thanks to @madstap. Switched to using fn over def and #(), and pr over println.
((fn f[i](try(f(inc i))(catch Error e(pr i))))0)
Wrote and tested on my phone. The Clojure REPL app gave me a depth of 13087.
Basic solution. Recurse until a SO is thrown, incrementing a counter each recurse. When it's thrown, the value of the counter is printed.
pr instead of println. Also -2 bytes by making the fn like this: ((fn f[x](,,,))0) instead of (def f #(,,,))(f 0).
Function A:On Error Resume Next:A=A()+1
Call using ?A() in the Immediate window, or as worksheet function.
Note: Returns 4613 in Excel-VBA, while the answer by @Greedo returns 3666 on my system (highest should be the max). Apparently also varies between Office programs (Access-VBA returns 4622, Word-VBA 4615)
Edit: Guess VBA auto-adds parantheses, so removed them.
L.xyhbbyZ
If I can run it like the J answer above, this is 7 bytes because you can take out the last yZ.
764, but you're right most of the time it gives no output.
Loops until it hits the limit.
: m 1+ recurse ;
: f 0 ['] m catch drop ; f .
END{p$.}
$stderr=$<
f=->{$.+=1;f[]}
f[]
Suppressing the error message is a little shorter than rescuing it, since by default rescue doesn't catch SystemStackError.
There's a cheesier answer if I can output in unary, representing n with n consecutive newline characters:
$stderr=$<
f=->{puts;$.+=1;f[]}
f[]
:( *
“¡żuẋ×HẒpƙ7"8!ƭ»ŒV
* Since Jelly as far as I am aware:
(1) sets the Python recursion limit prior to setting up much of its own interpreter and parsing the code to be run; and
(2) has no way of catching Python errors
I'm not sure if there is a way to either reliably evaluate the recursion limit or to print it out as it is discovered other than to actually ask Python what the value was set to (I'd love to see if it can be done though!) so that's what the code here does:
“¡żuẋ×HẒpƙ7"8!ƭ»ŒV - Link: no arguments
“¡żuẋ×HẒpƙ7"8!ƭ» - compression of "sys."+"get"+"recursion"+"limit"+"()"
ŒV - evaluate as Python code