C# 查找第 N 个根

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18657508/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-10 12:52:01  来源:igfitidea点击:

C# Find Nth Root

c#nth-root

提问by illusion

I use below method to calculate Nth Root of double value, but it takes a lot of time for calculating the 240th root. I found out about Newton method, but was not able to implement it into a method. Any help would be appreciated.

我使用下面的方法来计算 double 值的 Nth Root,但是计算 240th 根需要很多时间。我发现了牛顿法,但无法将其实现为方法。任何帮助,将不胜感激。

static double NthRoot(double A, int N)
{
   double epsilon = 0.00001d;//
   double n = N;
   double x = A / n;
   while (Math.Abs(A-Power(x,N)) > epsilon)
   {
    x = (1.0d/n) * ((n-1)*x + (A/(Power(x, N-1))));
   }
   return x;
}

采纳答案by Stefan Dragnev

static double NthRoot(double A, int N)
{
    return Math.Pow(A, 1.0 / N);
}

From Wikipedia:

来自维基百科

In calculus, roots are treated as special cases of exponentiation, where the exponent is a fraction:

在微积分中,根被视为求幂的特殊情况,其中指数是一个分数:

\sqrt[n]{x} \,=\, x^{1/n} 

回答by Abhas Bhoi

You can use the same function used to find the power of a number, just use reciprocal of the number instead of the number itself.

您可以使用与用于求数字的幂相同的函数,只需使用数字的倒数而不是数字本身。

To find N root of X you can write,

要找到 X 的 N 个根,你可以这样写,

int root = Convert.ToInt32(Math.Pow(X, (1 / N));