The problem
You are given a whole number n, which is 0 or more. Return the largest whole number r with r * r <= n. This is the square root of n, rounded down. Your answer must be exact, even for numbers much bigger than a float can store.
isqrt(16) # 4
isqrt(17) # 4
isqrt(10**30 - 1) # 999999999999999
math.isqrt is switched off for this problem. Build it yourself with binary search.
Limits
nis 0 or more, with up to about 41 digits.- Return an
int.
Builds on BSRC-5 Real-valued binary search and BSRC-6 Off-by-one traps.
Continue on a laptop
Practice runs Python in the browser and needs a keyboard and a wider screen. Read the problem and open hints here, then write your solution on a laptop. Progress is saved per browser for now, so hints opened here stay on this device.
Need a hint?0 of 3 opened
- Hint 1Nudge
- Hint 2Approach Open hint 1 first
- Hint 3Pseudocode Open hint 2 first
- Worked solutionlast resort Opens after all 3 hints
Ctrl + Enter runs the tests. Tab indents; press Esc, then Tab, to leave the editor.
| # | Test | Result | Reads · time |
|---|---|---|---|
| 1 | Zero | Not run | |
| 2 | One | Not run | |
| 3 | Two | Not run | |
| 4 | Just below a square | Not run | |
| 5 | A perfect square | Not run | |
| 6 | Just above a square | Not run | |
| 7 | A trillion | Not run | |
| 8 | Floats get this one wrong | Not run | |
| 9 | A 41-digit number | Not run | |
| 10 | Just past 2**53 | Not run | |
| 11 | A huge number that is not a square | Not run |