Challenge: Integer Square Root
Completed
Read n and print the largest whole number r with r × r ≤ n. Use binary search on the answer – no math.sqrt, no ** 0.5.
Input
One whole number n (0 ≤ n ≤ 1018).
Output
The integer square root.
Example
Input
50
Output
7
Editor
Python
Hints
The answer is between 0 and n.
If mid * mid <= n, the answer is at least mid; otherwise it's below mid.
Use mid = (lo + hi + 1) // 2 so the loop always makes progress.
Solution explanation
Even for n = 10¹⁸ this takes about 60 steps.
Show solution
n = int(input())
lo, hi = 0, n
while lo < hi:
mid = (lo + hi + 1) // 2
if mid * mid <= n:
lo = mid
else:
hi = mid - 1
print(lo) Output
Answer
You can practice without logging in. Log in to save completion.
Save your progress
You're practising as a guest, so your progress won't be saved.
- Track every lab you complete
- Earn verifiable lab certificates
- See your progress in every module