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
function check ( A , B , C , D , x ) { var ans ; ans = ( A * x * x * x + B * x * x + C * x + D ) ; return ans ; } function findSolution ( A , B , C , D , E ) { var start = 0 , end = 100000 ; var mid , ans ; while ( start <= end ) { mid = parseInt ( start + ( end - start ) / 2 ) ; ans = check ( A , B , C , D , mid ) ; if ( ans == E ) { document . write ( mid ) ; return ; } if ( ans < E ) start = mid + 1 ; else end = mid - 1 ; } document . write ( " " ) ; } var A = 1 , B = 0 , C = 0 ; var D = 0 , E = 27 ; findSolution ( A , B , C , D , E ) ;