अधिकतम पुनरावृत्ति गहराई [बंद]


15

क्या आपकी भाषा में अधिकतम पुनरावृत्ति गहराई (MRD) है?

मान लीजिए कि आपकी भाषा में MRD = 500 है

एक कोड लिखें जो पुनरावृत्ति की गहराई का पता लगाता है और सटीक मूल्य को आउटपुट करता है

आपके प्रोग्राम (या फ़ंक्शन) के ऊपर के मामले के लिए 500 आउटपुट होना चाहिए

कोड-गोल्फ सबसे छोटी उत्तर जीत!


3
@cairdcoinheringaahing ... "जो पुनरावृत्ति की गहराई का पता लगाता है" का अर्थ है

6
मुझे लगता है कि इस चुनौती के साथ मुख्य समस्या यह है कि हार्डकोड कोड को प्रिंट करने की अनुमति नहीं है, लेकिन हार्डकोड सिस्टम वेरिएबल पढ़ना ठीक है। दोनों वास्तव में मुझे बहुत भिन्न नहीं लगते हैं।
जेम्स

2
@DJMcMayhem बिल्ट-इन कई बार हार्डकोड जानकारी का उपयोग करता है। यह चुनौती बिल्ट-इन की अनुमति देती है।

7
हां, यह मेरी बात है। वे दोनों केवल एक हार्डकोड मूल्य पढ़ रहे हैं, लेकिन एक की अनुमति है और दूसरा नहीं है।
जेम्स

3
@DJMcMayhem में अंतर्निहित गणितज्ञ के पास भी स्विस ध्वज हो सकता है (मैंने यह चुनौती यहां देखी है), लेकिन jpg के अमान्य होने पर उसी ध्वज को पोस्ट करना।

जवाबों:



19

पायथन 3 , 40 बाइट्स

def f(x=2):
 try:f(x+1)
 except:print(x)

इसे ऑनलाइन आज़माएं!

बिना बिलियन से सिर्फ इसे पढ़े। हम 1 के बजाय 2 से शुरू करते हैं क्योंकि त्रुटियों को छोड़कर खंड एक स्तर से पहले चलाया जाता है। यह निश्चित रूप से अजगर 2 में एक बाइट है।


1
क्या आपको कॉल करने के लिए 3 बाइट्स की आवश्यकता नहीं है
8bitwide

फ़ंक्शन के रूप में @ 8bit प्रस्तुतियाँ डिफ़ॉल्ट रूप से स्वीकार की जाती हैं। जब तक प्रश्न विशेष रूप से उन पर प्रतिबंध नहीं लगाता तब तक आप समाधान के रूप में पुन: प्रयोज्य कार्य प्रस्तुत कर सकते हैं। आप देखेंगे कि इस प्रश्न के कई अन्य उत्तर भी यही काम करते हैं।
FryAmTheEggman 14

15

जावास्क्रिप्ट (बैबेल) , 35 33 29 बाइट्स

f=_=>do{try{-~f()}catch(e){}}
  • 2 बाइट्स ने नील को धन्यवाद दिया।

इसे यहाँ आज़माएँ , या evalइसके बजाय इसका परीक्षण करने के लिए नीचे स्निपेट का उपयोग करें do

console.log((f=_=>eval(`try{-~f()}catch(e){}`))())


जैप पोर्ट, 24 बाइट्स

यह वास्तव में इसे एक अलग समाधान के रूप में पोस्ट करने के लायक नहीं है क्योंकि यह अनिवार्य रूप से समान है।

Ox`try\{-~rp()}¯t®(e)\{}

झसे आज़माओ


व्याख्या

जावास्क्रिप्ट में स्वयं के प्रति एक पुनरावृत्ति सीमा नहीं है, बल्कि सीमा दुभाषिया (यानी, ब्राउज़र) द्वारा लगाई जाती है - अच्छी बात है कि हम उनके दुभाषिया के दौर द्वारा भाषाओं को परिभाषित करते हैं! अन्य कारकों में, सीमा ब्राउज़र और उपलब्ध मेमोरी द्वारा भिन्न हो सकती है, जो कि प्रदर्शन किए जा रहे कार्यों से प्रभावित होती है। निम्नलिखित स्निपेट उस अंतिम बिंदु को दिखाता है, जिसके समाधान के 5 अलग-अलग संस्करणों का उपयोग करके मैं गया था। जैसा कि आप पिछले 2 परीक्षणों से देख सकते हैं, क्रोम में, कम से कम, यहां तक ​​कि संचालन के क्रम में भी फर्क पड़ सकता है।

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खुद को बुलाया। हम द्वारा त्रुटि से बचने कर सकते हैं trying कॉल करने के लिए fलगातार और catching जब यह विफल:

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){}`)

अच्छा समाधान, क्योंकि मैं मान रहा हूं कि आपके पास JS में एक रिकर्सन डेप्थ बिल्टिन तक पहुंच नहीं है!
Zacharý

3
33 बाइट्स:f=_=>eval('try{-~f()}catch(e){}')
नील

@ नील: मैंने आपके 34 बाइट संस्करण देखे क्योंकि मैं बिस्तर पर जा रहा था और इसके बारे में न सोचने के लिए खुद को लात मारी। वह 33 बाइट संस्करण से प्रेरित है। धन्यवाद।
झबरा

13

गणितज्ञ (कोई अंतर्निहित), 20 बाइट्स

#0[#+1];&@1
%[[1,1]]

;गणना करने की गणना करना 1+$IterationLimit(शायद इसलिए कि गणितज्ञ पूंछ-कार्य को अनुकूलित कर सकते हैं)। वैकल्पिक रूप से डिफ़ॉल्ट की 0 //. x_ -> x + 1गणना करें , अर्थात, (जो ऊपर दिए गए दोनों मानों से बड़ा है)।ReplaceRepeatedMaxIteration65536

(यह एक कोड स्निपेट है जो परिणाम का मूल्यांकन करता है। हालांकि अन्य गणितज्ञ समाधान भी है)


10

जे, 8 बाइट्स

1+$: ::]

इसे ऑनलाइन आज़माएं!

इसलिए, मैं वास्तव में यह नहीं जानता कि बिना किसी इनपुट के क्रिया को कैसे निष्पादित किया जाए और कुछ संक्षिप्त खोज (साथ ही व्यक्तिगत अंतर्ज्ञान) से ऐसा लगता है कि यह संभव नहीं है। यदि यह है, तो कृपया मुझे बताएं कि यह कैसे करना है और मैं अपना उत्तर हटा दूंगा या अपडेट करूंगा। यह वास्तव में क्रिया के लिए कोई इनपुट नहीं होने का कोई मतलब नहीं है, हालांकि। इसके प्रकाश में, दिए गए फ़ंक्शन को उम्मीद है 0, पूर्णांक के लिए डिफ़ॉल्ट "खाली" इनपुट। मैं शायद इसे खाली सरणी ( 0$0) का उपयोग करने के लिए बदल सकता हूं यदि आपको लगता है कि यह अधिक उपयुक्त है।

संपादित करें: ओपी ने फ़ंक्शन को 0 लेने की अनुमति दी है।

व्याख्या

1+$: ::]
     ::]  Assign adverse: if an error occurs, call ] (the identify function)
1+        Add one to
  $:      Recursive call to self

यह स्वयं को पुनरावर्ती कहता है, इनपुट में 1 (0 अपेक्षित) जोड़ते हुए जब तक यह एक स्टैक त्रुटि नहीं मारता। जब यह त्रुटि करता है, तो यह ]इनपुट पर प्रतिकूल ( सटीक पहचान) कहता है , जो कि अभी 0 है।

वैसे, अंतरिक्ष आवश्यक है


1
मेरी मशीन पर 6000 का उत्पादन करता है। fwiw मुझे लगता है कि यह उचित खेल होना चाहिए, लेकिन आप हमेशा अपना जवाब दे सकते हैं(1+$: ::]) 0
योना

@ जोनाहा उचित बिंदु, मुझे कार्यों को प्रस्तुत करने के लिए उपयोग किया जाता है। मेरी मशीन पर, यह 6666 विचित्र रूप से पर्याप्त है।
कोल

आईपैड समर्थक पर 6660। ठंडा!
अगंजु

जिस तरह से यह अधिकतम पुनरावृत्ति की गहराई को संभालता है वह संस्करण पर निर्भर लगता है - मेरे फोन पर मुझे 5999 मिलता है (जो कि 1 से बंद होता है)। मेरे iPad पर (ईमानदारी से मुझे याद नहीं है कि कौन सा मॉडल), यह बस दुर्घटनाग्रस्त हो जाता है।
कोल

9

पायथन 3 , 41 32 बाइट्स

import sys
sys.getrecursionlimit

इसे ऑनलाइन आज़माएं!

सहेजे गए 9 बाइट @FryAmTheEggman की बदौलत!

34 बाइट्स

from sys import*
getrecursionlimit

35 बाइट्स

__import__('sys').getrecursionlimit

अंतिम 2 @totallyhuman के लिए धन्यवाद हैं


32 बाइट्स , 34 बाइट्स और 35 बाइट्स । अपना चयन ले लो। : पी
पूरी तरह से

@FryAmTheEggman हाँ, मैं आपको धन्यवाद दे सकता हूँ!
caird coinheringaahing

जब मैं पहली बार चलाने की कोशिश कर रहा हूं तो मुझे एक त्रुटि (टीआईओ पर, कम से कम) मिल रही है।
झबरा

@ शैगी आपको पहले एक के लिए लाइनों की अदला-बदली करनी होगी, बिल्टइन को एक नाम आवंटित करने की अनुमति देने के लिए बाद में आयात करना होगा। मैं लिंक को अपडेट करूंगा।
केयर्ड सिक्काहिंगाहिंग

8

सी (जीसीसी, लिनक्स एक्स 64), 180 133 बाइट्स

-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)));}

इसे ऑनलाइन आज़माएं!

एक SIGSEGV (सिग्नल 11) हैंडलर को एक वैकल्पिक सिग्नल स्टैक के साथ स्थापित करता है (न्यूनतम आकार MINSIGSTKSZ2 KB है, ध्वज SA_ONSTACK0x08000000 है), फिर बिना किसी तर्क के एक फ़ंक्शन को कॉल करता है और स्टैक ओवरफ्लो तक कोई स्थानीय चर पुनरावृत्ति नहीं करता है। यह दिलचस्प है कि अधिकतम पुनरावृत्ति की गहराई रन भर में भिन्न होती है, शायद एएसएलआर के कारण।

सी में अधिकतम पुनरावृत्ति गहराई बहुत सारे कारकों पर निर्भर करती है, निश्चित रूप से। एक विशिष्ट 64-बिट लिनक्स सिस्टम पर डिफ़ॉल्ट स्टैक का आकार 8 एमबी है, और स्टैक संरेखण 16 बाइट्स है, इसलिए आपको सरल कार्यों के लिए लगभग 512K की पुनरावृत्ति गहराई मिलती है।

यह भी ध्यान दें कि -O2टेल कॉल ऑप्टिमाइज़ेशन के कारण ऊपर का प्रोग्राम काम नहीं करता है ।


1
+1! आप वेतन वृद्धि cऔर कॉल exitकरके और sigactionमापदंडों के रूप में 4 बाइट्स बचा सकते हैं । यह परिणाम पर ध्यान देने योग्य अंतर नहीं करता है: TIO लिंक
स्कूटनीट

6

जावा 8, 131 51 48 47 43 बाइट्स

int d;int c(){try{c();}finally{return++d;}}

-80 बाइट्स थैंक्स टू @ नवे । मैंने प्रोग्राम के बजाए एक विधि की कोशिश की, लेकिन एक गलती की जिससे पूर्ण कार्यक्रम समाप्त हो गया .. अब यह एक विधि है।
-3 बाइट्स के बजाय का उपयोग करके @Neil को धन्यवाद । -5 बाइट थैंक्स टू @ नवे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

1
51 बाइट्स:int c(){try{return-~c();}catch(Error e){return 1;}}
नेवई

2
@Nevay आप अक्सर टिप्पणियों में उत्कृष्ट उत्तर देते हैं। आप उन्हें उत्तर के रूप में पोस्ट कर सकते हैं, और कुछ प्रतिष्ठा प्राप्त कर सकते हैं। कई जावा उत्तर होने से किसी भी प्रश्न के लिए कुछ भी निषिद्ध नहीं है। ;-)
ओलिवियर ग्रेजायर

2
int c(){int n=1;try{n=-~c();}finally{return n;}}3 बाइट्स बचाता है, लेकिन मुझे एक अलग जवाब देता है?
नील

2
47 बाइट्स:int c(){int n=1;try{n+=c();}finally{return n;}}
नेवई

1
43 बाइट्स:int d;int c(){try{c();}finally{return++d;}}
नेवई


4

आर , 32 26 18 बाइट्स

-8 बाइट्स स्वेन होहेंस्टीन के लिए धन्यवाद : $आंशिक मिलान करेंगे, इसलिए हम expपूर्ण के बजाय बस का उपयोग कर सकते हैं expressions

cat(options()$exp)

optionsआदेश भी प्रत्यावर्तन गहराई, यानी सेट करने के लिए इस्तेमाल किया जा सकता options(expressions=500)500 के लिए।

इसे ऑनलाइन आज़माएं!


1
आंशिक मिलान के कारण हटाकर आप सात बाइट्स बचा सकते हैं । ressions$
स्वेन होइनस्टीन

1
योगदान के रूप में भविष्य के संदर्भ के लिए अधिक; क्या आम सहमति है कि आपको इसे बिल्ली () में लपेटने की आवश्यकता है? आर ज्यादातर परिस्थितियों में कुछ उत्पादन करेगा , तो वहाँ एक पोस्ट कहीं अच्छा अभ्यास / तर्क का पालन करने के लिए स्पष्ट है?
क्रिमिनल

@SvenHohenstein ख़तरा, मैं हमेशा इस बारे में भूल जाता हूँ कि मैं अच्छे स्टाइल में R कोड लिखने के बाद ... धन्यवाद!
ग्यूसेप

1
@CriminallyVulgar इस पोस्ट के लिए मेटा में देखें; वहाँ निश्चित रूप से इसके बारे में कुछ अनिश्चितता है।
Giuseppe

4

ऑक्टेव , 25 22 20 बाइट्स

2 bytes removed thanks to a suggestion by Sanchises

@max_recursion_depth

Anonymous function that outputs the value.

Try it online!


You don't need the (), as max_recursion_depth is also a function.
Sanchises

@Sanchises Thanks! You are right: even if the doc says it's a variable, it's actually a function
Luis Mendo

Your edit has turned this in a duplicate of the other Octave answer, hence my retained @ to keep it distinct (defining a function rather than REPL'ing the result).
Sanchises

@Sanchises Actually I just changed that, although for a different reason (the code should actually define a function)
Luis Mendo

Yeah the other answer is more like a program; I'm not sure if that should actually require 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)
Sanchises

3

zsh, 24 bytes

f(){f $[++i];f};set -x;f

Try it online! (See under debug)

bash, 24 bytes

f(){ f $[++i];};set -x;f

Try it online! (See under debug)

ksh93, 27 bytes

f(){ f $(($1+1));};set -x;f

Try it online! (See under debug)

dash, 27 bytes

f(){ f $(($1+1));};set -x;f

Try it online! (Exceeds tio debug output, run it in your own shell)


1
Should i=0 and the echo not be included in your byte count?
Shaggy

@Shaggy: Perhaps, I have changed it to a more self-contained solution
Thor

1

Lua, 52 bytes

f=load"b,e=pcall(f,(...or 3)+1)return b and e or..."

Try it online!


@Shaggy in this case yes, because I use the name f. If this wasn't recursive I could get away with not having it
ATaco

Ah, I didn't spot the f in pcall.
Shaggy

why does your program stops at 200? here you can see that in that simple function it goes beyond 200. if you remove the -- you can confirm that it is still a recursive call with no optimizations
Felipe Nardi Batista

1

q/kdb+, 16 bytes

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

1

Excel-VBA, 26 Bytes

?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:

Excel + Excel-Vba, 3 + 38 = 41 Bytes

Function f:f=Application.MaxIterations

As that can be called from a cell with

=f(

For VBA with no built in:

Excel-VBA, 53 44 40 bytes

-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)



1

Clojure, 72 55 48 bytes

-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.


You can save 5 bytes by using pr instead of println. Also -2 bytes by making the fn like this: ((fn f[x](,,,))0) instead of (def f #(,,,))(f 0).
madstap

@madstap Thanks. I'll make the changes in a bit.
Carcigenicate

1

VBA, any type, 41 39 bytes

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.


0

Pyth - 9 bytes

L.xyhbbyZ

If I can run it like the J answer above, this is 7 bytes because you can take out the last yZ.

Try it online here.


5
This doesn't work for me. Offline, I get a segmentation fault. Online, I get no output at all. You can't catch a segfault.
isaacg

@isaacg wait this is really weird. Online, it rarely gives 764, but you're right most of the time it gives no output.
Maltysen




0

Ruby, 39 bytes

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:

Ruby, 35 bytes

$stderr=$<
f=->{puts;$.+=1;f[]}
f[]

0

Jelly, 18 bytes

:( *

“¡żuẋ×HẒpƙ7"8!ƭ»ŒV

Try it online!

How?

* 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
हमारी साइट का प्रयोग करके, आप स्वीकार करते हैं कि आपने हमारी Cookie Policy और निजता नीति को पढ़ और समझा लिया है।
Licensed under cc by-sa 3.0 with attribution required.