Search code examples
c#nth-root

C# Find Nth Root


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.

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

Solution

  • 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}