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.
- 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
- Print a number containing K digits with digital root D
- Find Nth positive number whose digital root is X
- Find root of a number using Newton's method
- Primitive root of a prime number n modulo n
- Square root of a number without using sqrt() function
- 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
- Babylonian method for square root
- Program to calculate Root Mean Square
- Numbers in a Range with given Digital Root