Find the integral roots of a given Cubic equation | Function to find the value at x of the given equation ; Find the value equation at x ; Return the value of ans ; Function to find the integral solution of the given equation ; Initialise start and end ; Implement Binary Search ; Find mid ; Find the value of f ( x ) using current mid ; Check if current mid satisfy the equation ; Print mid and return ; Print " NA " if not found any integral solution ; Driver Code ; Function Call
def check ( A , B , C , D , x ) : NEW_LINE INDENT ans = 0 ; NEW_LINE ans = ( A * x * x * x + B * x * x + C * x + D ) ; NEW_LINE return ans ; NEW_LINE DEDENT def findSolution ( A , B , C , D , E ) : NEW_LINE INDENT start = 0 ; end = 100000 ; NEW_LINE mid = 0 ; NEW_LINE ans = 0 ; NEW_LINE while ( start <= end ) : NEW_LINE INDENT mid = start + ( end - start ) // 2 ; NEW_LINE ans = check ( A , B , C , D , mid ) ; NEW_LINE if ( ans == E ) : NEW_LINE INDENT print ( mid ) ; NEW_LINE return ; NEW_LINE DEDENT if ( ans < E ) : NEW_LINE INDENT start = mid + 1 ; NEW_LINE DEDENT else : NEW_LINE INDENT end = mid - 1 ; NEW_LINE DEDENT DEDENT print ( " NA " ) ; NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT A = 1 ; B = 0 ; C = 0 ; NEW_LINE D = 0 ; E = 27 ; NEW_LINE findSolution ( A , B , C , D , E ) ; NEW_LINE DEDENT