Problem 90
Integer Square Root
Easy · 10 pts · Math, Binary Search
Problem
You are given a non-negative integer N. Print the largest integer x such that x × x <= N, which is the square root of N rounded down. Do not use a built-in square root function, because floating-point rounding can give wrong answers for large N. Binary search over x from 0 to about 10^9 (since N <= 10^18): if mid × mid <= N, the answer is at least mid; otherwise it is smaller. Be careful that mid × mid does not overflow a 64-bit integer.
Input
A single integer N.
Output
Print floor(sqrt(N)).
Constraints
0 <= N <= 10^18
Example input
8
Example output
2
Explanation
For N = 8: 2 × 2 = 4 is at most 8, but 3 × 3 = 9 is more than 8. So the largest valid x is 2.