Problem 557
Lattice Points Inside a Circle
Medium · 20 pts · Math, Geometry
Problem
Count the points (x, y) with integer coordinates that lie inside or on the circle of radius R centred at the origin, that is, with x² + y² <= R². For each integer x from -R to R, the allowed y values are the integers from -h to h, where h = floor(√(R² - x²)), which gives 2h + 1 points. Compute h with an exact integer square root, because floating-point square roots can be off by one for large values. The numbers in this problem can be larger than a 32-bit int can hold (about 2.1 × 10^9), so use a 64-bit integer type: long long in C++, long in Java (Python needs nothing special).
Input
A single integer R.
Output
Print the number of lattice points with x² + y² <= R².
Constraints
0 <= R <= 1000000
Example input
1
Example output
5
Explanation
For R = 1 the points are (0,0), (1,0), (-1,0), (0,1) and (0,-1). Points like (1,1) have x² + y² = 2 > 1, so the answer is 5.