Least root of given quadratic equation for value greater than equal to K | Python3 program for the above approach ; Function to calculate value of quadratic equation for some x ; Function to calculate the minimum value of x such that F ( x ) >= K using binary search ; Start and end value for binary search ; Binary Search ; Computing F ( mid ) and F ( mid - 1 ) ; If F ( mid ) >= K and F ( mid - 1 ) < K return mid ; If F ( mid ) < K go to mid + 1 to end ; If F ( mid ) > K go to start to mid - 1 ; If no such value exist ; Given coefficients of Equations ; Find minimum value of x
import math NEW_LINE def func ( A , B , C , x ) : NEW_LINE INDENT return ( A * x * x + B * x + C ) NEW_LINE DEDENT def findMinx ( A , B , C , K ) : NEW_LINE INDENT start = 1 NEW_LINE end = math . ceil ( math . sqrt ( K ) ) NEW_LINE while ( start <= end ) : NEW_LINE INDENT mid = start + ( end - start ) // 2 NEW_LINE x = func ( A , B , C , mid ) NEW_LINE Y = func ( A , B , C , mid - 1 ) NEW_LINE if ( x >= K and Y < K ) : NEW_LINE INDENT return mid NEW_LINE DEDENT elif ( x < K ) : NEW_LINE INDENT start = mid + 1 NEW_LINE DEDENT else : NEW_LINE INDENT end = mid - 1 NEW_LINE DEDENT DEDENT return - 1 NEW_LINE DEDENT A = 3 NEW_LINE B = 4 NEW_LINE C = 5 NEW_LINE K = 6 NEW_LINE print ( findMinx ( A , B , C , K ) ) NEW_LINE