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.
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.
- 8086 program to find the square root of a perfect square root number | Set-2
- Find if there is a pair in root to a leaf path with sum equals to root's data
- Fifth 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.
- Square root of a number using log
- Find cubic root of a number
- Print a number containing K digits with digital root D
- Root to leaf path sum equal to a given number in BST
- Root to leaf path sum equal to a given number
- Primitive root of a prime number n modulo n
- Number of elements smaller than root using preorder traversal of a BST
- 8085 program to find square root of a number
- How to bind to port number less than 1024 with non root access?
- 8086 program to find Square Root of a number
- Find square root of number upto given precision using binary search
Improved By : jit_t