human
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"
How can the above be solved in Python?
assistant
#Here is code to check if a given number is a Perfect square using Binary Search and the other steps required.
 def checkPerfectSquare ( N , start , last ) :
	mid = int ( ( start + last ) / 2 )
	if ( start > last ) :
		return - 1
	if ( mid * mid == N ) :
		return mid
	elif ( mid * mid > N ) :
		return checkPerfectSquare ( N , start , mid - 1 )
	else :
		return checkPerfectSquare ( N , mid + 1 , last )
N = 65
print ( checkPerfectSquare ( N , 1 , N ) )