हम कारकों को खोजने के लिए रिकर्सन का उपयोग कर रहे हैं और स्टैक ओवरफ्लो अपवाद प्राप्त कर रहे हैं। हमने पढ़ा है कि the C# compiler on x64 computers performs tail call optimizations:पूंछ कॉल अनुकूलन क्यों नहीं हो रहा है?
जेआईटी अनुकूलित कोड चलाते समय निश्चित रूप से tailcals करता है और डीबगिंग नहीं करता है।
हमारे कार्यक्रम में अब तक इस हो जाता है चल रहा है dotnet --configuration release
:
...
7214 is a factor of 1234567890
7606 is a factor of 1234567890
10821 is a factor of 1234567890
11409 is a factor of 1234567890
Process is terminated due to StackOverflowException.
पूंछ कॉल अनुकूलन क्यों उत्पन्न नहीं कर रहा है?
class Program
{
static void Main(string[] args)
{
const long firstCandidate = 1;
WriteAllFactors(1234567890, firstCandidate);
}
private static void WriteAllFactors(long number, long candidate)
{
if (number % candidate == 0)
{
System.Console.WriteLine($"{candidate} is a factor of {number}");
}
candidate = candidate + 1;
if(candidate > number/2)
{
return;
}
WriteAllFactors(number, candidate);
}
}
पूंछ कॉल रिकर्सन की आवश्यकता नहीं है कि आप मूल्य वापस कर दें? – zzzzBov
@zzzzBov मैं अनुमान लगा रहा हूं कि यह एक अशिष्ट सवाल है। :) –
कोई वापसी नहीं है; क्या यह हमेशा के लिए दौड़ने से रोकता है? –