Check if a number is perfect square without finding square root | Program to find if x is a perfect square . ; Check if mid is perfect square ; Mid is small -> go right to increase mid ; Mid is large -> to left to decrease mid ; Driver code ; Function Call
def isPerfectSquare ( x ) : NEW_LINE INDENT left = 1 NEW_LINE right = x NEW_LINE while ( left <= right ) : NEW_LINE INDENT mid = ( left + right ) >> 1 NEW_LINE if ( ( mid * mid ) == x ) : NEW_LINE INDENT return True NEW_LINE DEDENT if ( mid * mid < x ) : NEW_LINE INDENT left = mid + 1 NEW_LINE DEDENT else : NEW_LINE INDENT right = mid - 1 NEW_LINE DEDENT DEDENT return False NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT x = 2500 NEW_LINE if ( isPerfectSquare ( x ) ) : NEW_LINE INDENT print ( " Yes " ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( " No " ) NEW_LINE DEDENT DEDENT