Find Lattice Points in the Circle

Last Updated : 16 Aug, 2026

Given an integer r, representing the radius of a circle centered at the origin (0, 0), find the total number of lattice points lying on the circumference of the circle. A lattice point is a point in 2-D space whose coordinates are both integers.

Examples: 

Input: r = 5
Output: 12
Explanation: The lattice points are (0,5), (0,-5), (5,0), (-5,0), (3,4), (-3,4), (-3,-4), (3,-4), (4,3), (-4,3), (-4,-3), (4,-3).

Input: r = 88
Output: 4
Explanation: The lattice points are (0,88), (88,0), (0,-88), (-88,0).

Try It Yourself
redirect icon

[Naive Approach] Check All Points - O(r ^ 2) Time and O(1) Space

The idea is to check every integer point (x, y) inside the square [-r, r] × [-r, r]. If it satisfies x² + y² = r², then it lies on the circumference.

Working of Approach:

  • Iterate x from -r to r.
  • For every x, iterate y from -r to r.
  • Check whether x² + y² = r².
  • If true, increment the count.
  • Return the total count.
C++
#include <iostream>
#include <cmath>
using namespace std;

int latticePoints(int r)
{

    int cnt = 0;

    // Check all possible integer coordinates.
    for (int x = -r; x <= r; x++)
    {
        for (int y = -r; y <= r; y++)
        {

            // Check whether the point lies on the circle.
            if (1LL * x * x + 1LL * y * y == 1LL * r * r)
            {
                cnt++;
            }
        }
    }

    return cnt;
}

int main()
{

    int r = 88;

    cout << latticePoints(r) << endl;

    return 0;
}
Java
import java.util.*;

public class GFG {
    public static int latticePoints(int r) {
        int cnt = 0;
        // Check all possible integer coordinates.
        for (int x = -r; x <= r; x++) {
            for (int y = -r; y <= r; y++) {
                // Check whether the point lies on the circle.
                if ((long) x * x + (long) y * y == (long) r * r) {
                    cnt++;
                }
            }
        }
        return cnt;
    }
    public static void main(String[] args) {
        int r = 88;
        System.out.println(latticePoints(r));
    }
}
Python
def latticePoints(r):
    cnt = 0
    # Check all possible integer coordinates.
    for x in range(-r, r + 1):
        for y in range(-r, r + 1):
            # Check whether the point lies on the circle.
            if x * x + y * y == r * r:
                cnt += 1
    return cnt


if __name__ == "__main__":
    r = 88
    print(latticePoints(r))
C#
using System;

public class GFG {
    public static int latticePoints(int r)
    {
        int cnt = 0;
        // Check all possible integer coordinates.
        for (int x = -r; x <= r; x++) {
            for (int y = -r; y <= r; y++) {
                // Check whether the point lies on the
                // circle.
                if ((long)x * x + (long)y * y
                    == (long)r * r) {
                    cnt++;
                }
            }
        }
        return cnt;
    }
    public static void Main()
    {
        int r = 88;
        Console.WriteLine(latticePoints(r));
    }
}
JavaScript
function latticePoints(r)
{
    let cnt = 0;

    // Check all possible integer coordinates.
    for (let x = -r; x <= r; x++) {
        for (let y = -r; y <= r; y++) {

            // Check whether the point lies on the circle.
            if (BigInt(x) * BigInt(x)
                    + BigInt(y) * BigInt(y)
                == BigInt(r) * BigInt(r)) {
                cnt++;
            }
        }
    }

    return cnt;
}

// Driver Code
let r = 88;
console.log(latticePoints(r));

Output
4

[Expected Approach] Using Circle Symmetry - O(r) Time and O(1) Space

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 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.

Working of Approach:

  • Start with the 4 points on the axes: (±r, 0) and (0, ±r).
  • For every x from 1 to r-1, calculate y² = r² - x².
  • Find y = sqrt(y²) and check whether it is an integer.
  • Each valid (x, y) gives 4 symmetric points.
  • Return the total count.

Let us understand with an example:
Input: r = 88

  • Initially, res = 4 for the four axis points: (88,0), (-88,0), (0,88), (0,-88).
  • The loop checks every x from 1 to 87 and calculates y² = 88² - x².
  • For every x from 1 to 87, y² is not a perfect square, so no additional lattice points are found.
  • Therefore, res remains 4 throughout the loop.
  • The function returns 4, representing the four axis points.
C++
#include <iostream>
#include <cmath>
using namespace std;

int latticePoints(int r)
{
    // No lattice points exist on a circle of radius 0
    if (r == 0)
    {
        return 0;
    }

    // Four axis points: (r,0), (-r,0), (0,r), (0,-r)
    int res = 4;

    // Check all possible x-coordinates
    for (int x = 1; x < r; x++)
    {

        // Compute y² using the circle equation: x² + y² = r²
        int ySquare = r * r - x * x;

        // Find the integer part of sqrt(y²)
        int y = sqrt(ySquare);

        // If y² matches exactly, then (x, y) is a lattice point
        // Count all four symmetric points
        if (y * y == ySquare)
        {
            res += 4;
        }
    }

    return res;
}

int main()
{

    int r = 88;

    cout << latticePoints(r) << endl;

    return 0;
}
Java
import java.lang.Math;

public class GFG {
    public static int latticePoints(int r)
    {
        // No lattice points exist on a circle of radius 0
        if (r == 0) {
            return 0;
        }

        // Four axis points: (r,0), (-r,0), (0,r), (0,-r)
        int res = 4;

        // Check all possible x-coordinates
        for (int x = 1; x < r; x++) {

            // Compute y² using the circle equation: x² + y²
            // = r²
            int ySquare = r * r - x * x;

            // Find the integer part of sqrt(y²)
            int y = (int)Math.sqrt(ySquare);

            // If y² matches exactly, then (x, y) is a
            // lattice point Count all four symmetric points
            if (y * y == ySquare) {
                res += 4;
            }
        }

        return res;
    }

    public static void main(String[] args)
    {
        int r = 88;
        System.out.println(latticePoints(r));
    }
}
Python
import math


def latticePoints(r):
    # No lattice points exist on a circle of radius 0
    if r == 0:
        return 0

    # Four axis points: (r,0), (-r,0), (0,r), (0,-r)
    res = 4

    # Check all possible x-coordinates
    for x in range(1, r):
        # Compute y² using the circle equation: x² + y² = r²
        ySquare = r * r - x * x

        # Find the integer part of sqrt(y²)
        y = int(math.sqrt(ySquare))

        # If y² matches exactly, then (x, y) is a lattice point
        # Count all four symmetric points
        if y * y == ySquare:
            res += 4

    return res


if __name__ == '__main__':
    r = 88
    print(latticePoints(r))
C#
using System;

class GFG {
    static int latticePoints(int r)
    {
        // No lattice points exist on a circle of radius 0
        if (r == 0) {
            return 0;
        }

        // Four axis points: (r,0), (-r,0), (0,r), (0,-r)
        int res = 4;

        // Check all possible x-coordinates
        for (int x = 1; x < r; x++) {
            // Compute y² using the circle equation: x² + y²
            // = r²
            int ySquare = r * r - x * x;

            // Find the integer part of sqrt(y²)
            int y = (int)Math.Sqrt(ySquare);

            // If y² matches exactly, then (x, y) is a
            // lattice point Count all four symmetric points
            if (y * y == ySquare) {
                res += 4;
            }
        }

        return res;
    }

    static void Main()
    {
        int r = 88;
        Console.WriteLine(latticePoints(r));
    }
}
JavaScript
function latticePoints(r)
{
    // No lattice points exist on a circle of radius 0
    if (r === 0) {
        return 0;
    }

    // Four axis points: (r,0), (-r,0), (0,r), (0,-r)
    let res = 4;

    // Check all possible x-coordinates
    for (let x = 1; x < r; x++) {

        // Compute y² using the circle equation: x² + y² =
        // r²
        let ySquare = r * r - x * x;

        // Find the integer part of sqrt(y²)
        let y = Math.floor(Math.sqrt(ySquare));

        // If y² matches exactly, then (x, y) is a lattice
        // point Count all four symmetric points
        if (y * y === ySquare) {
            res += 4;
        }
    }

    return res;
}

// Driver Code
console.log(latticePoints(88));

Output
4
Comment