简体   繁体   English

Heron 算法递归 C#

[英]Heron Algorithm Recursive C#

I'm trying to implement the heron algorithm recursively in C#. I don't really understand where my code is wrong:我试图在C#中递归地实现苍鹭算法。我不太明白我的代码哪里错了:

Given definition of algorithm:给定算法的定义:

x[n+1] = (p-1) /p*x[n] + a/p*x[n]^p-1
Where xo = 1 and p root a
public static double Heron(int x,int p,int a)
{
    if(x == 0)
    {
        return 1.0;
    }

    return ((p-1.0)/p)*Heron(--x,p,a)+a/(p*Math.Pow(Heron(--x,p,a),--p));

}

eg Heron(1,3,5) should return 7/3;例如 Heron(1,3,5) 应该返回 7/3;

Don't modify the value of x and p in your expression.不要修改表达式中xp的值。

Just use只需使用

double x_n = Heron(x-1,p,a);
return ((p-1.0)/p)*x_n+a/(p*Math.Pow(x_n,p-1));

I have also kept the value of the recursion in x_n so it does not get called twice.我还在x_n中保留了递归的值,因此它不会被调用两次。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM