Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C# Find Nth Root

Tags:

c#

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;
}
like image 961
illusion Avatar asked Sep 06 '13 12:09

illusion


People also ask

What C is used for?

C programming language is a machine-independent programming language that is mainly used to create many types of applications and operating systems such as Windows, and other complicated programs such as the Oracle database, Git, Python interpreter, and games and is considered a programming foundation in the process of ...

What is the full name of C?

In the real sense it has no meaning or full form. It was developed by Dennis Ritchie and Ken Thompson at AT&T bell Lab. First, they used to call it as B language then later they made some improvement into it and renamed it as C and its superscript as C++ which was invented by Dr.

What is C in C language?

What is C? C is a general-purpose programming language created by Dennis Ritchie at the Bell Laboratories in 1972. It is a very popular language, despite being old. C is strongly associated with UNIX, as it was developed to write the UNIX operating system.

Is C language easy?

C is a general-purpose language that most programmers learn before moving on to more complex languages. From Unix and Windows to Tic Tac Toe and Photoshop, several of the most commonly used applications today have been built on C. It is easy to learn because: A simple syntax with only 32 keywords.


2 Answers

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} 
like image 126
Stefan Dragnev Avatar answered Sep 18 '22 05:09

Stefan Dragnev


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,

int root = Convert.ToInt32(Math.Pow(X, (1 / N)); 
like image 26
Abhas Bhoi Avatar answered Sep 18 '22 05:09

Abhas Bhoi