Find Nth root of a number using Bisection method | Function that returns the value of the function at a given value of x ; calculating the value of the differential of the function ; The function that returns the root of given number ; Defining range on which answer can be found ; finding mid value ; Driver code
function f ( x , p , num ) { return Math . pow ( x , p ) - num ; } function f_prime ( x , p ) { return p * Math . pow ( x , p - 1 ) ; } function root ( num , p ) { let left = - num ; let right = num ; let x ; while ( true ) { x = ( left + right ) / 2.0 ; let value = f ( x , p , num ) ; let prime = f_prime ( x , p ) ; if ( value * prime <= 0 ) left = x ; else right = x ; if ( value < 0.000001 && value >= 0 ) { return x ; } } } let P = 1234321 ; let N = 2 ; let ans = Math . floor ( root ( P , N ) ) ; document . write ( ans ) ;