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
def f ( x , p , num ) : NEW_LINE INDENT return pow ( x , p ) - num NEW_LINE DEDENT def f_prime ( x , p ) : NEW_LINE INDENT return p * pow ( x , p - 1 ) NEW_LINE DEDENT def root ( num , p ) : NEW_LINE INDENT left = - num NEW_LINE right = num NEW_LINE x = 0 NEW_LINE while ( True ) : NEW_LINE INDENT x = ( left + right ) / 2.0 NEW_LINE value = f ( x , p , num ) NEW_LINE prime = f_prime ( x , p ) NEW_LINE if ( value * prime <= 0 ) : NEW_LINE INDENT left = x NEW_LINE DEDENT else : NEW_LINE INDENT right = x NEW_LINE DEDENT if ( value < 0.000001 and value >= 0 ) : NEW_LINE INDENT return x NEW_LINE DEDENT DEDENT DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT P = 1234321 NEW_LINE N = 2 NEW_LINE ans = root ( P , N ) NEW_LINE print ( ans ) NEW_LINE DEDENT