您的语言是否具有最大递归深度(MRD)?
假设您的语言的MRD = 500
编写代码以找到递归深度并输出精确值
对于上述情况,您的程序(或函数)应输出500
打高尔夫球 最短答案胜出!
您的语言是否具有最大递归深度(MRD)?
假设您的语言的MRD = 500
编写代码以找到递归深度并输出精确值
对于上述情况,您的程序(或函数)应输出500
打高尔夫球 最短答案胜出!
Answers:
def f(x=2):
try:f(x+1)
except:print(x)
不只是从内置文件中读取它。我们从2开始而不是1,因为except子句在错误发生之前运行了一层。当然,这在python 2中要短一个字节。
f=_=>do{try{-~f()}catch(e){}}
在这里尝试,或使用下面的代码片段eval代替进行测试do。
console.log((f=_=>eval(`try{-~f()}catch(e){}`))())
实际上,这是完全相同的,因此不值得将此作为单独的解决方案发布。
Ox`try\{-~rp()}¯t®(e)\{}
JavaScript本身本身没有递归限制,而是由解释器(即浏览器)强加了限制-很好的是,我们在这里通过其解释器来定义语言!在其他因素中,限制可能会因浏览器和可用内存而异,这受执行的操作影响。下面的代码片段使用我经历的5个不同版本的解决方案说明了最后一点。从最近的两次测试中可以看出,至少在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调用了多少次。我们可以通过tryingf连续调用并catch在失败时ing来避免错误:
f=_=>{try{f()}catch(e){}}
没有错误,但是仍然没有返回该函数在失败之前成功调用自身多少次的返回值,因为该catch函数实际上并没有做任何事情。让我们尝试评估该try / catch语句:
f=_=>eval(`try{f()}catch(e){}`)
现在,我们返回了一个值(并且由于这是代码高尔夫,因此使用了real节省了一些字节return)。但是,返回的值仍然是undefined,因为catch并未执行任何操作。幸运的是,我们-~undefined==1和-~n==n+1所以,通过弹出一个-~呼叫的前面f,我们已经基本上得到了-~-~ ... -~-~undefined,另一个-~每次调用前置,给我们的次数f被调用。
f=_=>eval(`try{-~f()}catch(e){}`)
f=_=>eval('try{-~f()}catch(e){}')
1+$: ::]
因此,我实际上不知道如何在没有任何输入的情况下执行动词,并且进行简短的搜索(以及个人直觉)会使这看起来像是不可能的。如果是这样,请告诉我该怎么做,然后我将删除或更新我的答案。但是,不给动词输入没有任何意义。鉴于此,给定的函数Expects 0是整数的默认“空”输入。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标识),它仅为0。
顺便说一句,空间是必要的。
(1+$: ::]) 0
import sys
sys.getrecursionlimit
@FryAmTheEggman节省了9个字节!
from sys import*
getrecursionlimit
__import__('sys').getrecursionlimit
最后2个感谢@totallyhuman
-4个字节,感谢@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)));}
安装具有备用信号堆栈(最小大小MINSIGSTKSZ为2 KB,标志SA_ONSTACK为0x08000000)的SIGSEGV(信号11)处理程序,然后递归调用无参数且无局部变量的函数,直到堆栈溢出。有趣的是,最大递归深度可能会由于ASLR而在运行中有所不同。
当然,C语言中的最大递归深度取决于很多因素。在典型的64位Linux系统上,默认堆栈大小为8 MB,并且堆栈对齐为16个字节,因此对于简单功能,您获得的递归深度约为512K。
另请注意,-O2由于尾调用优化,以上程序无法使用。
int d;int c(){try{c();}finally{return++d;}}
-80字节感谢@Nevay。我也尝试了方法而不是程序,但是犯了一个错误,所以最终得到了完整的程序。现在是方法。@Neil通过使用
-3个字节代替。
-5字节再次感谢@Nevay。finallycatch(Error e)
说明:
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;}}节省了3个字节,但给了我不同的答案?
int c(){int n=1;try{n+=c();}finally{return n;}}
int d;int c(){try{c();}finally{return++d;}}
感谢Sven Hohenstein,-8个字节:$将进行部分匹配,因此我们可以使用exp而不是full expressions。
cat(options()$exp)
该options命令还可以用于设置递归深度,即为options(expressions=500)500。
ressions$
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