VOOZH about

URL: https://www.geeksforgeeks.org/dsa/circle-lattice-points/

⇱ Circle and Lattice Points - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

Circle and Lattice Points

Last Updated : 23 Jul, 2025

Given a circle of radius r in 2-D with origin or (0, 0) as center. The task is to find the total lattice points on circumference. Lattice Points are points with coordinates as integers in 2-D space.
Example: 

Input : r = 5.
Output : 12
Below are lattice points on a circle with
radius 5 and origin as (0, 0).
(0,5), (0,-5), (5,0), (-5,0),
(3,4), (-3,4), (-3,-4), (3,-4),
(4,3), (-4,3), (-4,-3), (4,-3).
are 12 lattice point.



To find lattice points, we basically need to find values of (x, y) which satisfy the equation x2 + y2 = r2
For any value of (x, y) that satisfies the above equation we actually have total 4 different combination which that satisfy the equation. For example if r = 5 and (3, 4) is a pair which satisfies the equation, there are actually 4 combinations (3, 4) , (-3,4) , (-3,-4) , (3,-4). There is an exception though, in case of (0, r) or (r, 0) there are actually 2 points as there is no negative 0.


// Initialize result as 4 for (r, 0), (-r. 0),
// (0, r) and (0, -r)
result = 4

Loop for x = 1 to r-1 and do following for every x.
If r*r - x*x is a perfect square, then add 4
tor result.


Below is the implementation of above idea. 

Output: 

12

Time Complexity: O(r), where "r" is the radius of the circle.
Auxiliary Space: O(1)


Reference:
https://mathworld.wolfram.com/CircleLatticePoints.html

Comment
Article Tags:
Article Tags: