Find root of a number using Newton 's method | Function to return the square root of a number using Newtons method ; Assuming the sqrt of n as n only ; To count the number of iterations ; Calculate more closed x ; Check for closeness ; Update root ; Driver code
def squareRoot ( n , l ) : NEW_LINE INDENT x = n NEW_LINE count = 0 NEW_LINE while ( 1 ) : NEW_LINE INDENT count += 1 NEW_LINE root = 0.5 * ( x + ( n / x ) ) NEW_LINE if ( abs ( root - x ) < l ) : NEW_LINE INDENT break NEW_LINE DEDENT x = root NEW_LINE DEDENT return root NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT n = 327 NEW_LINE l = 0.00001 NEW_LINE print ( squareRoot ( n , l ) ) NEW_LINE DEDENT