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.
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
- Nth root of a number using log
- Square root of a number using log
- Find cubic root of a number
- Smallest root of the equation x^2 + s(x)*x - n = 0, where s(x) is the sum of digits of root x.
- C program to find square root of a given number
- Find Cube root of a number using Log function
- Find root of a number using Newton's method
- Primitive root of a prime number n modulo n
- Find Nth positive number whose digital root is X
- Print a number containing K digits with digital root D
- Square root of a number without using sqrt() function
- Floor value Kth root of a number using Recursive Binary Search
- Square root of a number by Repeated Subtraction method
- Min operations to reduce N by multiplying by any number or taking square root
- 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
- Fast inverse square root
- Check whether one root of the Quadratic Equation is twice of other or not