Check if a given number is a Perfect square using Binary Search | Function to check for perfect square number ; Find the mid value from start and last ; Check if we got the number which is square root of the perfect square number N ; If the square ( mid ) is greater than N it means only lower values then mid will be possibly the square root of N ; If the square ( mid ) is less than N it means only higher values then mid will be possibly the square root of N ; Driver code
def checkPerfectSquare ( N , start , last ) : NEW_LINE INDENT mid = int ( ( start + last ) / 2 ) NEW_LINE if ( start > last ) : NEW_LINE INDENT return - 1 NEW_LINE DEDENT if ( mid * mid == N ) : NEW_LINE INDENT return mid NEW_LINE DEDENT elif ( mid * mid > N ) : NEW_LINE INDENT return checkPerfectSquare ( N , start , mid - 1 ) NEW_LINE DEDENT else : NEW_LINE INDENT return checkPerfectSquare ( N , mid + 1 , last ) NEW_LINE DEDENT DEDENT N = 65 NEW_LINE print ( checkPerfectSquare ( N , 1 , N ) ) NEW_LINE