Runtime exception, recursion too deep

后端 未结 12 1042
余生分开走
余生分开走 2020-12-19 04:59

I converted the pseudo-code here into C#, and have it recursively repeat 10,000 times. But I get a C# runtime error, StackOverflow Exception after 9217

12条回答
  •  一个人的身影
    2020-12-19 05:45

    Iterative version:

    public static double CalculatePi(int maxRecursion)
    {
        double result=1;
        for(int i=maxRecursion-1;i>=1;i--)
        {
            result=1+i/(2.0*i+1)*result;  
        }
        return result*2;
    }
    

提交回复
热议问题