Given two numbers N and A, find N-th root of A. In mathematics, Nth root of a number A is a real number that gives A, when we raise it to integer power N. These roots are used in Number Theory and other advanced branches of mathematics.
Refer Wiki page for more information.
Input : A = 81 N = 4 Output : 3 3^4 = 81
As this problem involves a real valued function A^(1/N) we can solve this using Newton’s method, which starts with an initial guess and iteratively shift towards the result.
We can derive a relation between two consecutive values of iteration using Newton’s method as follows,
according to newton’s method x(K+1) = x(K) – f(x) / f’(x) here f(x) = x^(N) – A so f’(x) = N*x^(N - 1) and x(K) denoted the value of x at Kth iteration putting the values and simplifying we get, x(K + 1) = (1 / N) * ((N - 1) * x(K) + A / x(K) ^ (N - 1))
Using above relation, we can solve the given problem. In below code we iterate over values of x, until difference between two consecutive values of x become lower than desired accuracy.
Below is the implementation of above approach:
Nth root is 3
This article is contributed by Utkarsh Trivedi. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
Don’t stop now and take your learning to the next level. Learn all the important concepts of Data Structures and Algorithms with the help of the most trusted course: DSA Self Paced. Become industry ready at a student-friendly price.
- Nth root of a number using log
- Square root of a number using log
- Smallest root of the equation x^2 + s(x)*x - n = 0, where s(x) is the sum of digits of root x.
- Find cubic root of a number
- Square root of a number without using sqrt() function
- Find Nth positive number whose digital root is X
- Primitive root of a prime number n modulo n
- Find root of a number using Newton's method
- C program to find square root of a given number
- Print a number containing K digits with digital root D
- Find Cube root of a number using Log function
- Floor value Kth root of a number using Recursive Binary Search
- Find square root of number upto given precision using binary search
- Fast method to calculate inverse square root of a floating point number in IEEE 754 format
- Check if a number is perfect square without finding square root
- Square root of an integer
- Numbers in a Range with given Digital Root
- Program to calculate Root Mean Square
- Fast inverse square root
- Check whether one root of the Quadratic Equation is twice of other or not