Home > Article > Backend Development > Python implements finding the square root of an integer based on binary search
The example in this article describes the method of finding the square root of an integer in Python based on binary search. Share it with everyone for your reference, the details are as follows:
x=int(raw_input('please input a int:')) if x<0: retrun -1 low=0 high=x ans=(low+high)/2.0 sign=ans while ans**2 !=x: if ans**2>x: high=ans else: low=ans ans=(low+high)/2.0 if sign==ans: break print ans
For more related articles about finding the square root of an integer in Python based on binary search, please pay attention to PHP Chinese website!