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.
Examples:
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:
C++
#include <bits/stdc++.h>
using namespace std;
double nthRoot( int A, int N)
{
double xPre = rand () % 10;
double eps = 1e-3;
double delX = INT_MAX;
double xK;
while (delX > eps)
{
xK = ((N - 1.0) * xPre +
( double )A/ pow (xPre, N-1)) / ( double )N;
delX = abs (xK - xPre);
xPre = xK;
}
return xK;
}
int main()
{
int N = 4;
int A = 81;
double nthRootValue = nthRoot(A, N);
cout << "Nth root is " << nthRootValue << endl;
return 0;
}
|
Java
class GFG
{
static double nthRoot( int A, int N)
{
double xPre = Math.random() % 10 ;
double eps = 0.001 ;
double delX = 2147483647 ;
double xK = 0.0 ;
while (delX > eps)
{
xK = ((N - 1.0 ) * xPre +
( double )A / Math.pow(xPre, N - 1 )) / ( double )N;
delX = Math.abs(xK - xPre);
xPre = xK;
}
return xK;
}
public static void main (String[] args)
{
int N = 4 ;
int A = 81 ;
double nthRootValue = nthRoot(A, N);
System.out.println( "Nth root is "
+ Math.round(nthRootValue* 1000.0 )/ 1000.0 );
}
}
|
Python3
import math
import random
def nthRoot(A,N):
xPre = random.randint( 1 , 101 ) % 10
eps = 0.001
delX = 2147483647
xK = 0.0
while (delX > eps):
xK = ((N - 1.0 ) * xPre +
A / pow (xPre, N - 1 )) / N
delX = abs (xK - xPre)
xPre = xK;
return xK
N = 4
A = 81
nthRootValue = nthRoot(A, N)
print ( "Nth root is " , nthRootValue)
|
C#
using System;
class GFG
{
static double nthRoot( int A, int N)
{
Random rand = new Random();
double xPre = rand.Next(10);;
double eps = 0.001;
double delX = 2147483647;
double xK = 0.0;
while (delX > eps)
{
xK = ((N - 1.0) * xPre +
( double )A / Math.Pow(xPre, N - 1)) / ( double )N;
delX = Math.Abs(xK - xPre);
xPre = xK;
}
return xK;
}
static void Main()
{
int N = 4;
int A = 81;
double nthRootValue = nthRoot(A, N);
Console.WriteLine( "Nth root is " +Math.Round(nthRootValue*1000.0)/1000.0);
}
}
|
PHP
<?php
function nthRoot( $A , $N )
{
$xPre = rand() % 10;
$eps = 0.001;
$delX = PHP_INT_MAX;
$xK ;
while ( $delX > $eps )
{
$xK = ((int)( $N - 1.0) *
$xPre + $A /
(int)pow( $xPre ,
$N - 1)) / $N ;
$delX = abs ( $xK - $xPre );
$xPre = $xK ;
}
return floor ( $xK );
}
$N = 4;
$A = 81;
$nthRootValue = nthRoot( $A , $N );
echo "Nth root is " ,
$nthRootValue , "\n" ;
?>
|
Javascript
<script>
function nthRoot(A, N)
{
let xPre = Math.random() % 10;
let eps = 0.001;
let delX = 2147483647;
let xK = 0.0;
while (delX > eps)
{
xK = ((N - 1.0) * xPre +
A / Math.pow(xPre, N - 1)) / N;
delX = Math.abs(xK - xPre);
xPre = xK;
}
return xK;
}
let N = 4;
let A = 81;
let nthRootValue = nthRoot(A, N);
document.write( "Nth root is " +Math.round(nthRootValue*1000.0)/1000.0);
</script>
|
Time Complexity: O(log(eps)), where eps is the desired accuracy.
Space Complexity: O(1)
If you like GeeksforGeeks and would like to contribute, you can also write an article using write.geeksforgeeks.org or mail your article to review-team@geeksforgeeks.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.
Feeling lost in the world of random DSA topics, wasting time without progress? It's time for a change! Join our DSA course, where we'll guide you on an exciting journey to master DSA efficiently and on schedule.
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 geeks!
Last Updated :
23 Mar, 2023
Like Article
Save Article