educative.io

Educative

What's wrong with my solution? I can't figure it out

def integer_square_root(k):

low = 0

high = k

while low < high:

mid = (low + high)//2

if mid*mid < = k and (mid+1)*(mid+1)>k:

  return mid

elif mid*mid > k:

  high = mid -1

else:

  low = mid +1

Course: Educative: Interactive Courses for Software Developers
Lesson: Educative: Interactive Courses for Software Developers

hey @jessie_Zhao your code is working absolutely fine.

For most of the numbers the code is working correctly. For some numbers where the calculated high number is the square root. Here the else part was getting executed till low is same has high number. This is a correct square root. Here the condition low < high was not allowing one additional run of loop to get this exact square root.

By making condition while low < = high, the program will give correct number.